Thread overview
new struct initializer syntax
Jan 30, 2004
Sean L. Palmer
Jan 30, 2004
Vathix
Jan 30, 2004
Manfred Nowak
Jan 30, 2004
Sean L. Palmer
January 30, 2004
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


January 30, 2004
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?
January 30, 2004
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.
January 30, 2004
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
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?