Fixing C's Biggest Mistake

Max Samukha maxsamukha at gmail.com
Sat Dec 31 10:28:42 UTC 2022


On Friday, 30 December 2022 at 20:44:04 UTC, Walter Bright wrote:
> What I meant was default construction, which is not necessary 
> in D.

It is necessary. For types that require runtime construction, 
initializing to T.init does not result in a constructed object. 
Forcing programmers to use factory functions doesn't make much 
sense:

struct S
{
     this() @disable;
}

S s() { S r = ...; return r; } // you disallowed `S()` just to 
make people fake it.


There is no need to forbid the nullary constructor. I 
intentionally don't call it "default constructor", because I want 
it to be distinct from initializing to T.init. I want this:

@disable(init) // or whatever syntax you prefer
struct S
{
     this() {}
}

S s; // error: explicit constructor call required
S s = S(); // ok, explicit constructor call
S s = S.init; // shouldn't be allowed in safe code

S[] ss;
ss.length = 1; // error
S[] ss = [S()]; // ok
// etc.

I would still dislike it, but it at least would save me from the 
factory function nonsense.


More information about the Digitalmars-d mailing list