Jump to page: 1 2
Thread overview
Proposal: struct and array literal syntax
Jun 20, 2006
Chris Miller
Jun 20, 2006
Alexander Panek
Jun 20, 2006
Lars Ivar Igesund
Jun 20, 2006
Tom S
Jun 21, 2006
Boris Wang
Jun 21, 2006
Derek Parnell
Jun 21, 2006
Andrei Khropov
Jun 23, 2006
Boris Wang
Jun 23, 2006
Andrei Khropov
Jun 23, 2006
Bruno Medeiros
Jun 24, 2006
Daniel Keep
Jun 24, 2006
Andrei Khropov
Jun 24, 2006
Chris Miller
Jun 25, 2006
Andrei Khropov
Jun 25, 2006
Bruno Medeiros
Jun 26, 2006
Stewart Gordon
Jun 23, 2006
BCS
June 20, 2006
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.

- Chris
June 20, 2006
I like the idea very much, as it's like:
"Use MyStruct as a 'template' for this data!{...}"

Chris ftw! (Got my vote ;) )

Regards,
Alex

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.
> 
> - Chris
June 20, 2006
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.
> 
> - Chris

It is no doubt that we need some good syntax for initalizing structs and arrays, and this might as good as any other suggestion. I like Alex' thoughts on it too.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource & #D: larsivi
June 20, 2006
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.

I like it a lot. Readable and functional.


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
June 20, 2006
"Chris Miller" <chris@dprogramming.com> wrote in message news:op.tbft9dffpo9bzi@moe...
> 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.

I like it, and it doesn't seem to introduce any ambiguity in the grammar, since if you're instantiating a templated struct, the ! will be immediately followed by a left paren, and not a left brace.  And of course you never use ! with arrays.  It also solves the problem of parsing the array literal, since

int[][6]

Would be parsed as a type, while

int[]![6]

Would be parsed as an array literal.


June 21, 2006
It's Wonderfull, like it.

"Chris Miller" <chris@dprogramming.com> дÈëÏûÏ¢ÐÂÎÅ:op.tbft9dffpo9bzi@moe...
> 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.
>
> - Chris
> 


June 21, 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
June 21, 2006
Derek Parnell wrote:

> 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.
Well, in the last case inner int[]! s appear to be a syntactic overhead.

I'm still waiting for better handling of multidimensional rectangular arrays (not less effective "jagged").

-- 
AKhropov
June 23, 2006
"Andrei Khropov" <andkhropov@nospam_mtu-net.ru> дÈëÏûÏ¢ÐÂÎÅ:e7b7fb$277d$1@digitaldaemon.com...
> Derek Parnell wrote:
>
>> 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.
> Well, in the last case inner int[]! s appear to be a syntactic overhead.
>
> I'm still waiting for better handling of multidimensional rectangular
> arrays
> (not less effective "jagged").
>
> -- 
> AKhropov
>

May be:

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

the type of ![1,2,3,4] can be inferred.


June 23, 2006
Boris Wang wrote:

> 
> "Andrei Khropov" <andkhropov@nospam_mtu-net.ru> P4HkO{O"PBNE:e7b7fb$277d$1@digitaldaemon.com...
> > Derek Parnell wrote:
> > 
> > > 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.
> > Well, in the last case inner int[]! s appear to be a syntactic overhead.
> > 
> > I'm still waiting for better handling of multidimensional rectangular arrays (not less effective "jagged").
> > 
> > --  AKhropov
> > 
> 
> May be:
> 
> int[][]![
>     ![1,2,3,4],
>     ![5,6,7,8],
>     ![9,0,1,2],
>     ![3,4,5,6],
> ]
> 
> the type of ![1,2,3,4] can be inferred.

In the spirit of the recent type inference extensions the outer type should be inferred also: just

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

-- 
AKhropov
« First   ‹ Prev
1 2