Thread overview
Why does struct initializer works for arrays but not for associative arrays?
Mar 14, 2018
Andre Pany
Mar 14, 2018
Uknown
Mar 14, 2018
Jonathan M Davis
Mar 14, 2018
Seb
Mar 14, 2018
Andre Pany
March 14, 2018
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é
March 14, 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
    };
March 14, 2018
On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn 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é

Well, I think that you have two issues here:

1. Struct literals work in only a few, specific circumstances. Why, I don't know, but IIRC, someone was writing a DIP to fix that, and that may or may not fix this case. So, as I understand it, it's not all that hard to run into places where they don't work (I confess that I never use them, because I don't like them any more than I like the fact that struct constructors are implicitly declared if you don't declare them, which has caused me bugs when changing the members fo a struct; both are misfeatures IMHO, though obviously not everyone agrees on that point).

2. In general in D, the type of an expression is not inferred based on where it's used. There are a few exceptions where literals are involved, but in general, if you have something like

A a = expression;

expression has to evaluate correct on its own without taking A into account. The fact that something like

Bar b = {s: "str"};

compiles is actually a bit of an oddity in D's semantics in that respect. So, the fact that it works at all is a bit of a special case, and clearly, they didn't get everything. My guess is that the problem is that the dynamic array literal needs a type, but the compiler is not set up to figure out what type that is based on the fact that it's being used in a struct literal.

- Jonathan M Davis


March 14, 2018
On Wednesday, 14 March 2018 at 15:17:54 UTC, Jonathan M Davis wrote:
> On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn wrote:
>> [...]
>
> Well, I think that you have two issues here:
>
> 1. Struct literals work in only a few, specific circumstances. Why, I don't know, but IIRC, someone was writing a DIP to fix that, and that may or may not fix this case.

For reference, that's the DIP: https://github.com/dlang/DIPs/pull/71
March 14, 2018
On Wednesday, 14 March 2018 at 15:17:54 UTC, Jonathan M Davis wrote:
> On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn wrote:
>> [...]
>
> Well, I think that you have two issues here:
>
> 1. Struct literals work in only a few, specific circumstances. Why, I don't know, but IIRC, someone was writing a DIP to fix that, and that may or may not fix this case. So, as I understand it, it's not all that hard to run into places where they don't work (I confess that I never use them, because I don't like them any more than I like the fact that struct constructors are implicitly declared if you don't declare them, which has caused me bugs when changing the members fo a struct; both are misfeatures IMHO, though obviously not everyone agrees on that point).
>
> 2. In general in D, the type of an expression is not inferred based on where it's used. There are a few exceptions where literals are involved, but in general, if you have something like
>
> A a = expression;
>
> expression has to evaluate correct on its own without taking A into account. The fact that something like
>
> Bar b = {s: "str"};
>
> compiles is actually a bit of an oddity in D's semantics in that respect. So, the fact that it works at all is a bit of a special case, and clearly, they didn't get everything. My guess is that the problem is that the dynamic array literal needs a type, but the compiler is not set up to figure out what type that is based on the fact that it's being used in a struct literal.
>
> - Jonathan M Davis

Thanks for the information. As it works fine for dynamic arrays and the coding
in the compiler seems identical for arrays and associative arrays I really
wonder why it not work.

Although I really anticipate the DIP, this can be solved as bug fix.
The chances to solve this a bug fix are much higher than getting the DIP implemented
in near future (unfortunately).

Kind regards
André