www.digitalmars.com         C & C++   DMDScript  

D - new struct initializer syntax

reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
What about this:

struct Foo
{
    int bar, baz, bam;
};

Foo[] myfoo =  // D already has this stuff AFAIK
{
    { 0, 1, 2 }, // you don't have to use names, but then you are dependent
on order
    { bar = 3, baz = 4, bam = 5 }, // you can use names
    { bam = 8, baz = 7, bar = 6 }, // order doesn't matter when named
}

Foo[] myfoo2 =    // new part
    ( baz, bam, bar )        // gives the field order the following list
initializer uses
{
    { 1, 2, 0 },        // uses order given above
    { 4, 5, 3 },
    { baz = 7, bar = 6, bam = 8 } // you can still use explicit names if you
want
}

And as you can surmise, myfoo and myfoo2 end up containing identical data.

Whaddya think?  Not 100% sure parens are the best thing to use...  the
general idea is more important than the exact syntax.

Sean
Jan 30 2004
parent reply Vathix <vathix dprogramming.com> writes:
Sean L. Palmer wrote:

 What about this:
 
 struct Foo
 {
     int bar, baz, bam;
 };
 
 Foo[] myfoo =  // D already has this stuff AFAIK
 {
     { 0, 1, 2 }, // you don't have to use names, but then you are dependent
 on order
     { bar = 3, baz = 4, bam = 5 }, // you can use names
     { bam = 8, baz = 7, bar = 6 }, // order doesn't matter when named
 }
 
 Foo[] myfoo2 =    // new part
     ( baz, bam, bar )        // gives the field order the following list
 initializer uses
 {
     { 1, 2, 0 },        // uses order given above
     { 4, 5, 3 },
     { baz = 7, bar = 6, bam = 8 } // you can still use explicit names if you
 want
 }
 
 And as you can surmise, myfoo and myfoo2 end up containing identical data.
 
 Whaddya think?  Not 100% sure parens are the best thing to use...  the
 general idea is more important than the exact syntax.
 
 Sean
 

Well, you can do this: struct Foo { int bar, baz, bam; } Foo[] myfoo = [ { 0, 1, 2 }, { bar: 3, baz: 4, bam: 5 }, { bam: 8, baz: 7, bar: 6 }, ]; Don't know if I like your field ordering thing though; would it really get used, or confused?
Jan 30 2004
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Vathix wrote:

 Well, you can do this:

 Foo[] myfoo =  [
      { 0, 1, 2 },

Confirmed. But this should be a bug because the docs say: | If a static initializer is supplied, the members are initialized | by the member name, colon, expression syntax.
 Don't know if I like your field ordering thing though; would it
 really get used, or confused?

If the member names have to be mentioned, and I think they should, then in arrays they truly will be used to spare the typing of member names again and again. So long.
Jan 30 2004
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
It's for the cases where the struct author goes and changes the struct field
order after you wrote your initializer.  Keeps things from breaking.

Sean

"Vathix" <vathix dprogramming.com> wrote in message
news:bvdfvs$2o6s$1 digitaldaemon.com...
 Sean L. Palmer wrote:

 What about this:

 struct Foo
 {
     int bar, baz, bam;
 };

 Foo[] myfoo =  // D already has this stuff AFAIK
 {
     { 0, 1, 2 }, // you don't have to use names, but then you are


 on order
     { bar = 3, baz = 4, bam = 5 }, // you can use names
     { bam = 8, baz = 7, bar = 6 }, // order doesn't matter when named
 }

 Foo[] myfoo2 =    // new part
     ( baz, bam, bar )        // gives the field order the following list
 initializer uses
 {
     { 1, 2, 0 },        // uses order given above
     { 4, 5, 3 },
     { baz = 7, bar = 6, bam = 8 } // you can still use explicit names if


 want
 }

 And as you can surmise, myfoo and myfoo2 end up containing identical


 Whaddya think?  Not 100% sure parens are the best thing to use...  the
 general idea is more important than the exact syntax.

 Sean

Well, you can do this: struct Foo { int bar, baz, bam; } Foo[] myfoo = [ { 0, 1, 2 }, { bar: 3, baz: 4, bam: 5 }, { bam: 8, baz: 7, bar: 6 }, ]; Don't know if I like your field ordering thing though; would it really get used, or confused?

Jan 30 2004