ffi M1.0 (1/3): lock in expression-bodied top-level + struct-method form
sx's '=>' body form (already used for lambdas) works today for top-level function declarations and struct member methods. Pin the surface with examples/139-expression-bodied-fn.sx so a parser regression here surfaces immediately. Coverage: - module-top: double :: (x: s32) -> s32 => x * 2; - niladic: answer :: () -> s32 => 42; - struct method: total :: (self: *Point) -> s32 => self.x + self.y; Next: extend the same form to '#objc_class' member methods (the M2.1(a/b) class-constant + class-method overrides path).
This commit is contained in:
36
examples/139-expression-bodied-fn.sx
Normal file
36
examples/139-expression-bodied-fn.sx
Normal file
@@ -0,0 +1,36 @@
|
||||
// M1.0 — expression-bodied function declarations.
|
||||
//
|
||||
// sx's `=>` body form (already used for lambdas) extends to
|
||||
// top-level and struct-member function declarations:
|
||||
//
|
||||
// name :: (params) -> RetType => expr;
|
||||
//
|
||||
// Pins three positions: module-top, struct method, niladic.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
double :: (x: s32) -> s32 => x * 2;
|
||||
|
||||
sum :: (a: s32, b: s32) -> s32 => a + b;
|
||||
|
||||
answer :: () -> s32 => 42;
|
||||
|
||||
Point :: struct {
|
||||
x: s32;
|
||||
y: s32;
|
||||
|
||||
total :: (self: *Point) -> s32 => self.x + self.y;
|
||||
scaled :: (self: *Point, by: s32) -> s32 => (self.x + self.y) * by;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
print("double: {}\n", double(7));
|
||||
print("sum: {}\n", sum(3, 4));
|
||||
print("answer: {}\n", answer());
|
||||
|
||||
p := Point.{ x = 10, y = 20 };
|
||||
print("total: {}\n", p.total());
|
||||
print("scaled: {}\n", p.scaled(3));
|
||||
|
||||
0;
|
||||
}
|
||||
1
tests/expected/139-expression-bodied-fn.exit
Normal file
1
tests/expected/139-expression-bodied-fn.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
5
tests/expected/139-expression-bodied-fn.txt
Normal file
5
tests/expected/139-expression-bodied-fn.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
double: 14
|
||||
sum: 7
|
||||
answer: 42
|
||||
total: 30
|
||||
scaled: 90
|
||||
Reference in New Issue
Block a user