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?

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Jun 25 02:25:14 UTC 2024


On 25/06/2024 2:16 PM, mw wrote:
> struct shared_AA {
>    shared_AA_class saa = new shared_AA_class();  // by this syntax `saa` 
> is still instance variable?
>    alias saa this;
> }

When you specify an initializer like this, that instance of 
``shared_AA_class`` gets put into the .init of ``shared_AA``.

It is shared between instances, even if its a field.

```d
import std.stdio;

struct Foo {
     ubyte value = 2;
}

void main() {
     writeln(cast(ubyte[])__traits(initSymbol, Foo)); // [2]
}
```


More information about the Digitalmars-d-learn mailing list