Thread overview
can you initialize a array of POD structs?
Jun 18, 2022
Chris Katko
Jun 18, 2022
Adam D Ruppe
Jun 18, 2022
Chris Katko
June 18, 2022
struct pair { float x, y;}

pair p[] = [[0, 0], [255, 255], [25,-25]]; //nope
June 18, 2022
On Saturday, 18 June 2022 at 17:37:44 UTC, Chris Katko wrote:
> ````D
> struct pair { float x, y;}
>
> pair p[] = [[0, 0], [255, 255], [25,-25]]; //nope
> ````

An array of pair is `pair[]`, keep the brackets with the type.

Then a struct literal is either:

pair(0, 0) // using constructor syntax

or in some select contexts (including this one):

{0, 0} // using named literal syntax


Therefore:

pair[] p = [{0, 0}, {255, 255}, {25,-25}];

compiles here.
June 18, 2022
On Saturday, 18 June 2022 at 17:52:16 UTC, Adam D Ruppe wrote:
> On Saturday, 18 June 2022 at 17:37:44 UTC, Chris Katko wrote:
>> ````D
>> struct pair { float x, y;}
>>
>> pair p[] = [[0, 0], [255, 255], [25,-25]]; //nope
>> ````
>
> An array of pair is `pair[]`, keep the brackets with the type.
>
> Then a struct literal is either:
>
> pair(0, 0) // using constructor syntax
>
> or in some select contexts (including this one):
>
> {0, 0} // using named literal syntax
>
>
> Therefore:
>
> pair[] p = [{0, 0}, {255, 255}, {25,-25}];
>
> compiles here.

Thanks!!

One extra caveat for anyone who googles this. You can't use {0, 0} notation if the struct has constructors. Which make sense since you don't want people accidentally bypassing a constructor if it exists.