Files
sx/examples/closures/0315-closures-struct-field-call.sx
agra 959845bd30 style: migrate arrow-block lambdas () => { .. } to () { .. }
The canonical sx block-body lambda is `(params) { stmts }` (and
`(params) -> Ret { stmts }`); the arrow form `=>` is for EXPRESSION bodies
(`(params) => expr`). The arrow-block hybrid `(params) => { .. }` was being
used in 33 files — convert all of them by dropping the `=>`. The two forms are
exactly equivalent (verified: identical IR and identical runtime values — the
block tail is the value with or without a `-> Ret`), so this is a pure source
cleanup: no `.ir` churn, and the only snapshot change is 0923's diagnostic
COLUMN (a negative narrowing test whose error span shifted by the removed `=> `).

Arrow EXPRESSION bodies (`=> expr`, `=> .{..}`, `=> [..]`) and `=>` inside
comments/strings were left untouched. Migrated across examples/concurrency,
examples/{closures,ffi-objc,generics,optionals,types}, issues/, and the stdlib
(io.sx, sched.sx). Suite 855/0.
2026-06-28 16:39:51 +03:00

42 lines
1.4 KiB
Plaintext

// Calling a closure / function-pointer value stored in a STRUCT FIELD
// (`box.run(args)`) resolves the call's return type correctly — value returns
// marshal properly, and a failable field (`Closure(..) -> (T, !)`) is `try`/
// `catch`-able. The call-type resolver mirrors the lowering dispatch: a
// closure/fn-ptr field is called directly (and shadows a same-named method).
//
// Regression (issue 0201): the field-access call path typed such calls as
// `unresolved` — value returns came out as garbage, failable returns rejected
// `catch`/`try` ("operand has type 'unresolved'").
#import "modules/std.sx";
CB :: struct {
add: Closure(i64, i64) -> i64; // closure field, with args
fp: (i64) -> i64; // bare function-pointer field
work: Closure(i64) -> (i64, !); // failable closure field
}
triple :: (x: i64) -> i64 { return x * 3; }
// Field call through a `*CB` receiver inside a method, consuming the failable
// field's error channel.
run_work :: (self: *CB, n: i64) -> i64 {
v := self.work(n) catch { return -1; };
return v;
}
main :: () -> i64 {
b : CB = ---;
b.add = (x: i64, y: i64) => x + y;
b.fp = triple;
b.work = (n: i64) -> (i64, !) {
if n < 0 { raise error.Negative; }
n * 10
};
print("{}\n", b.add(3, 4)); // 7
print("{}\n", b.fp(5)); // 15
print("{}\n", run_work(@b, 6)); // 60
print("{}\n", run_work(@b, -1)); // -1 (error path)
return 0;
}