Jonathan M Davis <jmdavisp...@gmx.com> 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.

And you can't. That doesn't mean it can't be possible in the future.


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.

Indeed. That's been discussed here, and in short, these are the rules:

Arrays of non-null cannot have their lengths increased.

To create an array of non-null, use a literal, a function that checks
all parameters and builds an array from it, or a function that checks
all elements of a passed array of nullable elements.

To create a longer array, append two arrays.


> 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?

Default initialization is used all over the place - including both stuff like the declaration Foo f; and in array declarations. Every type is default initialized
when not initialized directly, and it has nothing to do with default
constructors. init must be known at compile time whereas default constructors
are run at runtime.

I mean, the compiler is doing something to move .init to the new instance
of the type, and this could very well be considered the default constructor.


init gets used a fair bit (such as when you declare an array of a given
size and the compiler _must_ initialize all of its elements), and it's
vital to how D functions.

static arrays would require initializers in this case:

NonNull!(T)[2]; // Compile-time error: NonNull!T has no default constructor

NonNull!(T)[2] = [nonNull(new T), nonNull(new T)]; // Works


Depending on exactly how init works, it could be a _huge_ change to try and make a default constructor run instead.

Possibly, though I don't think so. As said before, some code needs to be
executed to copy .init to the new instance. This code could likely be
replaced with a default constructor. Even so, the disabling of default
constructors is not really about constructors, but as you say, about
disabling .init. This will not require execution of new code, only
a compile-time check for whether the constructor has been disabled.

--
Simen

Reply via email to