Re: Field Initialiser Reused Across Object Instances

2021-01-01 Thread Adam via Digitalmars-d-learn
That's a fantastic answer! Thank you. I was not aware that initializers were always compile time, that was the missing piece in my understanding. It's a shame that I can't use the nicer (IMO) syntax, but the reasoning is sound.

Re: Field Initialiser Reused Across Object Instances

2021-01-01 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 1 January 2021 at 13:34:16 UTC, Adam wrote: A a = new A; // I would expect this to be called for each "new B". Your expectation is wrong for D. Since that's in a `static` context, it is run at compile time. Any variable marked `static`, `__gshared`, or is

Field Initialiser Reused Across Object Instances

2021-01-01 Thread Adam via Digitalmars-d-learn
Consider the following: class A { int x; } class B { A a = new A; // I would expect this to be called for each "new B". } void main() { import std.stdio; auto c = new B; writeln("c ", c.a.x); c.a.x++; writeln("c ", c.a.x); writeln; auto d = new B;