Proposal: struct and array literal syntax

Derek Parnell derek at nomail.afraid.org
Tue Jun 20 19:29:58 PDT 2006


On Tue, 20 Jun 2006 05:35:27 -0400, Chris Miller wrote:

> This is an old idea I had, and also applies to struct initializers which  
> now has ambiguity with literal delegates.
> 
> Given the following struct:
>     struct MyStruct { int foo; int bar; }
> can be created with the following proposed syntax:
>     MyStruct!{3, 4}
> which can be used in initializers and anywhere else MyStruct is expected.
> 
> For array initializers, see the following proposed syntax:
>     int[]![6, 7, 8, 9]
> which creates a dynamic array of 4 int elements.
> This as well can be used in initializers and anywhere else such an array  
> is expected.

Does it scale?

 struct BARBAR{ real bar; byte qwe; ulong rty; } ; 
 struct YourStruct { int foo; BARBAR q; }
 YourStruct!{3, BARBAR!{1.2234, 15, int.max} }

Now we try an nested *named* struct...

 struct MyStruct { int foo; 
                   struct BARBAR { int bar; char[] qwe; }  BARBAR ety; }
 MyStruct!{3, .BARBAR!{4, "carrot"} }

I tried "MyStruct.BARBAR!" at first but the redundant 'MyStruct' bothered
me a bit so I just used a '.' prefix to refer to the current struct
context.

Now we try an nested *unnamed* struct...

 struct MyStruct { int foo; struct { int bar; char[] qwe; } ; }
 MyStruct!{3, .!{4, "carrot"} }


Now we try multiple nested *unnamed* structs...

 struct MyStruct { int foo; struct { int bar; char[] qwe; } ; struct {int
why; int bother;} }
 MyStruct!{3, .!{4, "carrot"}, .!{0,1} }

Now an array of structs ...

 struct MyStruct { int foo; 
                   struct BARBAR { int bar; char[] qwe; }  BARBAR ety; }
 MyStruct[]![ 
     MyStruct!{3,  .BARBAR!{4, "carrot"} },
     MyStruct!{7,  .BARBAR!{6, "turnip"} },
     MyStruct!{13, .BARBAR!{2, "pea"} },
     MyStruct!{42, .BARBAR!{0, "bean"} },
          ]

Now a 'rectangular' array ...

  int[][]![
      int[]![1,2,3,4],
      int[]![5,6,7,8],
      int[]![9,0,1,2],
      int[]![3,4,5,6],
    ]

Hmmm ... appears to do okay. 

A great idea, Chris.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
21/06/2006 12:11:58 PM



More information about the Digitalmars-d mailing list