New structs
JMRyan
nospam at nospam.com
Fri Sep 10 10:27:26 PDT 2010
Consider this uninspiring pair structs:
struct S1 {int x};
struct S2
{
int x
this(int i) {x = i}
};
Note that no default constructor is allowed so that S2.init can have a
consistent value computed at compile time.
Now:
S1 a = S1(); // Quintessinal case works fine
S2 b = S2(); // Also works, D initializes b with S2.init
S2* c = new S1(); // Works, D initializes c* with S2.init
S2* d = new S2(); // Doesn't work: no default consructor
Instead of the last, we need:
S2* d = cast(S2*) GC.malloc(S2.sizeof);
d = S2(); // or: d = S2.init;
Is there any good reason why "S2* d = new S2();"
shouldn't be allowed? If allowed, D could initialize d* with S2.init.
S2 really isn't needed since S1(3) and S2(3) have the same effect.
Also, a final class would at least usually be just as good as a struct
here. But still, disallowing "S2* d = new S2();" seems decidedly
unnecessary, especially since "S2 b = S2();" already works.
More information about the Digitalmars-d
mailing list