how to initialize nested struct elegantly?

elvisxzhou elvis.x.zhou at gmail.com
Wed Dec 12 09:58:05 UTC 2018


On Wednesday, 12 December 2018 at 09:49:58 UTC, elvisxzhou wrote:
> import std.stdio;
>
> struct AA
> {
>     int i;
>     char c;
>     double d;
>     string s;
>     float f;
> }
>
> struct BB
> {
>     float f;
>     AA[2] a;
> }
>
> void print(BB* bb)
> {
>     writefln("bb.a[0].i=%d bb.f=%f", bb.a[0].i, bb.f);
> }
>
> void main()
> {
>     //dlang
>     AA aa = {
>         i:1010,
>         f:0.1f
>     };
>
>     BB bb = {
>         f : 0.2f,
>         a : [
>                 AA(1010, 0,0,null,0.1f),
>                 AA(1020,0,0,null,0.2f)
>             ]
>     };
>
>     print(&bb);
>
>     /************************************************/
>     /*
>
>     //C99
>     print( &(BB){
>         .f = 0.2f,
>         .a = {
>             [0] = { .i = 1010, .f = 0.1f }
>             [1] = { .i = 1020, .f = 0.2f }
>         }
>     });
>
>     */
> }
>
>
> dlang version has two unnecessary symbols aa and bb, which is 
> ugly compare to c99 one.

aa is already eliminated in the example, but you know where the 
ugly is.


More information about the Digitalmars-d-learn mailing list