Getting the default value of a class member field

WebFreak001 d.forum at webfreak.org
Thu Dec 1 08:09:05 UTC 2022


I've got this class definition:

```d
class X
{
     this()
     {
         assert(false);
     }

     int x = 3;
}
```

due to internal reasons the constructor would fail at compile 
time, so I put in an assert(false) here, and I can't add or 
change any methods in X.

How do I get `3` if I have `X` and field name `"x"` at compile 
time?

For structs `X.init.x` / `__traits(getMember, X.init, "x")` would 
work, however for classes it complains about null dereference.

I saw there is __traits(initSymbol), however that one doesn't 
work at compile time.


More information about the Digitalmars-d-learn mailing list