Is this D1 code? Because in D2: S2* c = new S1(); // Error: cannot implicitly convert expression (new S1) of type S1* to S2*
S2* d = cast(S2*) GC.malloc(S2.sizeof); d = S2(); // Error: cannot implicitly convert expression (S2(0)) of type S2 to S2* Neither of those work. On Fri, Sep 10, 2010 at 7:27 PM, JMRyan <[email protected]> wrote: > 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. >
