using .init reliably

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 26 05:40:04 PDT 2016


On Friday, August 26, 2016 11:20:56 Johan Engelen via Digitalmars-d-learn 
wrote:
> On Friday, 26 August 2016 at 09:48:00 UTC, Jonathan M Davis wrote:
> > You're supposed to be able to depend on .init existing. Default
> > initialization for structs can be disabled via
> >
> > @disable this();
> >
> > but even then, the init member still exists (it just isn't used
> > for default initialization).
>
>  From what I remember, the last time I looked at `@disable
> this();`:
> it prevents the user from creating a default initialized struct.
> However whenever (compiler internally) the struct needs
> initialization, `.init` is still used. And thus it is also used
> at the start of any constructor the user writes.
> Don't take my word for it: have a look at asm output, or easier:
> LCD's LLVM IR output.

I expect that that's true. The key thing is that if that if default
initialization is disabled, then you can't do something like

MyType mt;

But init itself is pretty integral to the language, so it's still needed for
other stuff. And it would be downright brutal if you couldn't depend on init
to exist for metaprogramming - which is what the OP was afraid of.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list