null [re: spec#]

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 8 10:16:53 PST 2010


On Monday, November 08, 2010 10:06:35 Walter Bright wrote:
> Jonathan M Davis wrote:
> > I was not aware of that. I didn't think that you could do that for
> > structs since default constructors are illegal in the first place. It
> > would likely have the negative side effect of making it illegal to put
> > Foo in arrays though, since it wouldn't be possible to fill in the array
> > values with init.
> 
> That's right.
> 
> The idea is to allow the declaration of a default constructor for structs,
> but only if it is marked as disabled. Then, any use of the struct that
> would require default initialization is disallowed. That includes static
> arrays, and arrays allocated via new.
> 
> A dynamic array could be constructed using a literal or by appending the
> values one by one.
> 
> This implies that a struct containing a field that has a disabled default
> constructor also cannot be default initialized.
> 
> Of course, you can defeat these protections by using a cast and
> encapsulating the code that does that. I believe Andrei was thinking of
> this when he mentioned using a template to initialize a nonnull array.

How hard would it really be to insert code wherever a struct's init value is 
used to default construct it either when the program starts up (for globals) or 
right after it's declared (for locals)? If you could do that, then I don't see 
why we couldn't have proper default constructors. All I can assume is that 
adding that extra code to default construct structs would be painful change to 
make to dmd, but perhaps there's something else that I'm missing. If that could 
be done though, it would be a _huge_ improvement.

- Jonathan M Davis


More information about the Digitalmars-d mailing list