sm
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user