Time to kill T() as (sometimes) working T.init alias ?

deadalnix deadalnix at gmail.com
Thu Nov 29 10:30:27 PST 2012


On Thursday, 29 November 2012 at 17:12:29 UTC, jerro wrote:
>> The original idea is that there should be *no such thing* as 
>> default construction of a struct as being anything other than 
>> T.init. The default construction of a struct should be a 
>> compile time creature, not a runtime one.
>>
>> Any methods or workarounds to try and make T() produce 
>> something different from T.init is bad D practice. The 
>> compiler tries to statically head them off, but probably 
>> should do a better job of that.
>
> The only reason to associate no parameter constructors with 
> default values is because of how C++ works. There is no reason 
> why
>
> Foo foo;                          (1)
>
> should be equivalent to
>
> auto foo = Foo();                 (2)
>
> in D. We could allow constructors with no parameters and make 
> (1) equivalent to
>
> auto foo = Foo.init;              (3)
>
> The current workaround when one wants (2) to construct the 
> object at runtime is to define a static opCall, but that's 
> messy and inconsistent. It's just one more quirk one needs to 
> learn to effectively use the language.

This do not solve problem with heap allocated struct being moved, 
but is definitively the right direction.


More information about the Digitalmars-d mailing list