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).
37 lines
843 B
Plaintext
37 lines
843 B
Plaintext
// 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;
|
|
}
|