Struct initialization is a mess
Dukc
ajieskola at gmail.com
Wed Jul 28 23:08:34 UTC 2021
In the `main` function, I have commented out the initialization
forms that don't compile. Everything else compiles.
```d
struct Basic
{ int cont;
}
struct NoDef
{ int cont;
@disable this();
}
struct FalseInit
{ int cont;
static float init;
}
struct Ctor
{ int cont;
this(int){}
}
struct EmptyOpcall
{ int cont;
static typeof(this) opCall(){return this.init;}
}
struct ArgumentedOpcall
{ int cont;
static typeof(this) opCall(int){return this.init;}
}
void main()
{ {Basic a, b = Basic.init, c = Basic(), d = {}, e = Basic(0), f
= {0};}
{NoDef /*a,*/ b = NoDef.init, /*c = NoDef(), d = {}, e =
NoDef(0),*/ f = {0};}
{FalseInit a, /*b = FalseInit.init,*/ c = FalseInit(), d = {},
e = FalseInit(0), f = {0};}
{Ctor a, b = Ctor.init, c = Ctor(),/*, d = {}*/ e = Ctor(0)/*,
f = {0}*/;}
{EmptyOpcall a, b = EmptyOpcall.init, c = EmptyOpcall(), d =
{}, /*e = EmptyOpcall(0)*/ f = {0};}
{ArgumentedOpcall a, b = ArgumentedOpcall.init, /*c =
ArgumentedOpcall(),*/ d = {}, e = ArgumentedOpcall(0), f = {0};}
}
```
We have terribly many ways to initialize a struct (or union or
class for that matter). I have trouble seeing the logic between
all these.
Now granted, many of these make sense. Obviously, non-explicit
initialization with `@disabled this()` is not supposed to
compile. And I'm being unfair with the `FalseInit` example,
defining that is just plain bad programming.
But still. Why `NoDef.init` compiles? Why C-style initialization
is okay with `opCall`ed `struct`s but not with ones that have
constructors? Why `Ctor()` is okay but `ArgumentedOpcall()` is
not?
If there is some big picture, I am failing to see it.
More information about the Digitalmars-d
mailing list