Files
sx/examples/issue-0007.sx
2026-02-24 19:31:10 +02:00

40 lines
937 B
Plaintext

// Issue: chained method call on struct field operates on a copy
// `a.field.method()` where method takes *Self creates a temporary copy of `field`
// instead of borrowing `a.field` as a pointer.
// The mutation is lost because it modifies the copy, not the original.
out :: (str: string) -> void #builtin;
Counter :: struct {
value: s64;
inc :: (self: *Counter) {
self.value += 1;
}
}
Parent :: struct {
counter: Counter;
}
main :: () {
p := Parent.{ counter = Counter.{ value = 0 } };
// This should increment p.counter.value, but the mutation is lost:
p.counter.inc();
if p.counter.value == 0 {
out("BUG: p.counter.value is still 0 after inc()\n");
} else {
out("OK: p.counter.value is 1\n");
}
// Workaround: take explicit pointer
cp := @p.counter;
cp.inc();
if p.counter.value == 1 {
out("OK: workaround via pointer works\n");
}
}