Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? `shared_AA.saa` should still be instance variable, not class variable, right?

mw mingwu at gmail.com
Tue Jun 25 21:36:16 UTC 2024


On Tuesday, 25 June 2024 at 21:13:44 UTC, Nick Treleaven wrote:
> On Tuesday, 25 June 2024 at 02:16:25 UTC, mw wrote:
>> Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? (and of 
>> course print out the same contents).
>>
>> `shared_AA.saa` should still be instance variable, not class 
>> variable, right?
>
> `saa` is an instance variable, but both `foo.x.saa` and 
> `foo.y.saa` are initialized to the same instance of 
> `shared_AA_class`. I think in the next edition of D we can 
> forbid tail mutable initializers.


I think the main problem here is that most people come to D from 
Java / C++ world (among the most popular languages). This tail 
initializer's syntax looks the same, but the semantics is so 
different and so surprisingly unexpected! (as I demo-ed in my 
previous reply).

At least, we need to have a document for Java / C++ programmers, 
which clearly highlights such important differences.



More information about the Digitalmars-d-learn mailing list