this() not executing code on structs

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Oct 23 08:40:33 PDT 2009


Denis Koroskin wrote:
> On Fri, 23 Oct 2009 18:46:47 +0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Don wrote:
>>> Bartosz Milewski wrote:
>>>> Andrei Alexandrescu Wrote:
>>>>
>>>>>     this() { myCount = count++; }       // ERROR
>>>>
>>>> It's worse than that. Try this:
>>>>
>>>> struct foo {
>>>>        this(int dummy = 0) { writeln("Default constructor");}
>>>> }
>>>>
>>>> foo x = foo();
>>>>
>>>> Nothing gets printed. If default constructors are disallowed, so 
>>>> should constructors with all parameters defaulted.
>>>  Ouch.
>>> It's because it's interpreting foo() as a struct literal.
>>> If a struct has any constructors, struct literals should be disabled.
>>
>> http://d.puremagic.com/issues/show_bug.cgi?id=3438
>>
>> The more I think of it, the more imperious it becomes that we allow 
>> default constructors that execute code. The main question is what to 
>> do about .init.
>>
>> Andrei
> 
> I'd suggest ditching it and enforce explicit member initialization 
> (unless a variable is nullable). This will also remove a lot of bloat 
> from executables.

I don't understand. The problem right now is that even of all fields are 
explicitly intialized, e.g.

struct A {
     Widget x = null;
     double d = 0;
     int i = -1;
}

there is still no ability to execute code upon initialization, e.g. 
force A to contain a non-null Widget.


Andrei



More information about the Digitalmars-d mailing list