null [re: spec#]

Simen kjaeraas simen.kjaras at gmail.com
Mon Nov 8 05:01:57 PST 2010


Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> ??? Structs have no default constructor.

True. Disabling the default constructor here means that

struct Foo{
     @disable this();
}
Foo f;

should halt compilation at Foo f;, and certain other places where a
default constructor would normally be used.


> They have an init property with is used for default initialization.

Indeed. And this is different from a default constructor that you can't
touch how?


> And if you could enforce that a struct be properly
> initialized rather than default initialized, that would be a big change.

Indeed. And that is what we are trying to achieve.


> Either init needs to be changed to allow for real default constructors  
> and/or objects need to be legal CTFE - ideally both.

Disabling the default constructor (which ostensibly looks this:
this( ) { this = init; }) is a very good step in the right direction.
Having proper default constructors would be better, and I can not
remember the reason we don't have that.


> So, there is not
> currently a way to have NotNull!T hold anything but null initially when  
> it's a
> member variable of a struct, and there's no sign of that changing any  
> time soon.

Indeed, and this will never change - it can't. However, we are arguing that
it could be null before the struct's constructor is called, and flow  
control
(or out contracts, if necessary) enforce that it is not null when the
constructor returns.

I admit I here somewhat miss C++'s initialization lists:

struct Foo {
     NonNull< Bar > bar;
     Foo( ) : bar( new Bar() ) {
     }
}


-- 
Simen


More information about the Digitalmars-d mailing list