Member field of type nested struct must be initialized in constructor: Why?

Paul Backus snarwin at gmail.com
Sun Oct 22 23:49:40 UTC 2023


On Sunday, 22 October 2023 at 21:02:32 UTC, Inkrementator wrote:
> Running the code with `rdmd -version=fix app.d` works, but 
> running `rdmd -version=fix app.d` produces:
> `Error: field `member` must be initialized in constructor, 
> because it is nested struct`
> Why?
> I didn't find anything about this in [the 
> spec.](https://dlang.org/spec/struct.html#nested)

Nested structs contain a context pointer that needs to be 
initialized at runtime. If you don't initialize them, the pointer 
gets set to `null`, and the struct will not be able to access its 
context.

Using `.init` doesn't fix this because `.init` is determined at 
compile time, and also has `null` in place of the context pointer.


More information about the Digitalmars-d-learn mailing list