Files
sx/examples/closures/0305-closures-closure-field-call-via-self-ptr.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

41 lines
875 B
Plaintext

// Invoking a Closure-typed struct field as `self.field()` from a
// method whose receiver is `*Self`. The field access must auto-deref
// the pointer before extracting the closure value.
#import "modules/std.sx";
Holder :: struct {
cb: Closure() = ---;
has: bool = false;
set :: (self: *Holder, fn: Closure()) {
self.cb = fn;
self.has = true;
}
// Direct invocation through *self.
call_direct :: (self: *Holder) {
if self.has == false { return; }
self.cb();
}
// Hoist-then-call form — must agree with the direct form.
call_hoisted :: (self: *Holder) {
if self.has == false { return; }
fn := self.cb;
fn();
}
}
ticks : i32 = 0;
main :: () -> i32 {
h : Holder = .{};
h.set(() => { ticks += 1; });
h.call_direct();
h.call_hoisted();
return ticks;
}