Unintentional sharing?
Nick Treleaven
nick at geany.org
Thu Jun 6 20:32:45 UTC 2024
On Thursday, 6 June 2024 at 17:49:39 UTC, Andy Valencia wrote:
> I was using instance initialization which allocated a new
> object. My intention was this initialization would happen
> per-instance, but all instances appear to share the same
> sub-object? That is, f1.b and f2.b appear to point to a single
> object? Obviously I moved the new into the initializer code,
> but I hadn't appreciated how initial instance values were
> calculated once. Interestingly, this makes it similar to how
> Python calculates default argument values for functions.
>
> class Bar {
> int z = 3;
> }
>
> class Foo {
> auto b = new Bar();
> }
>
> void
> main() {
> import std.stdio : writeln;
>
> auto f1 = new Foo(), f2 = new Foo();
> f1.b.z = 0;
> writeln(f2.b.z);
> }
This is a long standing issue:
https://issues.dlang.org/show_bug.cgi?id=2947
I think with the next edition we can disallow (tail) mutable
initializers for fields (and TLS globals too).
More information about the Digitalmars-d-learn
mailing list