Why does struct initializer works for arrays but not for associative arrays?

Uknown sireeshkodali1 at gmail.com
Wed Mar 14 15:13:03 UTC 2018


On Wednesday, 14 March 2018 at 13:36:51 UTC, Andre Pany wrote:
> Hi,
>
> I do not understand why struct initializer works for arrays but 
> not for
> associative arrays:
>
> struct Bar
> {
>     string s;
> }
>
> struct Foo
> {
>     Bar[string] asso;
>     Bar[] arr;
> }
>
> void main()
> {
>     Foo foo = {
>         arr: [{s: "123"}],
>         asso: ["0": {s: "123"}] // does not work
>     };
> }
>
> The coding for both types of arrays looks very similiar:
> https://github.com/dlang/dmd/blob/9ed779a7d68d2ac489338cc4758c10d0cb169b39/src/dmd/initsem.d#L634
>
> I cannot spot the difference.
>
> Kind regards
> André

This might just be a bug. Changing the initializer to an explicit 
call to Bar constructor compiles just fine

https://run.dlang.io/is/nuuolx

Even just doing

     Foo foo = {
         arr: [{s: "123"}],
         asso: ["0": {"123"}] // does not work
     };


More information about the Digitalmars-d-learn mailing list