www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - New structs

reply JMRyan <nospam nospam.com> writes:
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.
Sep 10 2010
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Is this D1 code? Because in D2:

S2* c =3D new S1(); //  Error: cannot implicitly convert expression (new
S1) of type S1* to S2*

S2* d =3D cast(S2*) GC.malloc(S2.sizeof);
d =3D 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 <nospam nospam.com> wrote:
 Consider this uninspiring pair structs:

 struct S1 {int x};
 struct S2
 {
 =A0 =A0int x
 =A0 =A0this(int i) {x =3D i}
 };

 Note that no default constructor is allowed so that S2.init can have a
 consistent value computed at compile time.

 Now:

 S1 a =3D S1(); =A0 =A0 =A0 // Quintessinal case works fine
 S2 b =3D S2(); =A0 =A0 =A0 // Also works, D initializes b with S2.init
 S2* c =3D new S1(); =A0// Works, D initializes c* with S2.init
 S2* d =3D new S2(); =A0// Doesn't work: no default consructor

 Instead of the last, we need:

 S2* d =3D cast(S2*) GC.malloc(S2.sizeof);
 d =3D S2(); =A0// or: d =3D S2.init;

 Is there any good reason why "S2* d =3D 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. =A0But still, disallowing "S2* d =3D new S2();" seems decidedly
 unnecessary, especially since "S2 b =3D S2();" already works.

Sep 10 2010
parent JMRyan <nospam nospam.com> writes:
"Steven Schveighoffer" <schveiguy yahoo.com> wrote in 
news:op.vitwqwoveav7ka localhost.localdomain:

 I think it was a typo.  He meant
 
 S1* c = new S1()
 
 BTW, I filed a bug report on this a while back:
 
 http://d.puremagic.com/issues/show_bug.cgi?id=4247
 
 -Steve

Yes, and the comment repeated the same typo. The whole line should have read: S1* c = new S1(); // Works, D initializes c* with S1.init I just voted for your bug report.
Sep 10 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 10 Sep 2010 13:55:37 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 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.

I think it was a typo. He meant S1* c = new S1() BTW, I filed a bug report on this a while back: http://d.puremagic.com/issues/show_bug.cgi?id=4247 -Steve
Sep 10 2010