Struct literals and AA literals

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 23 23:59:18 PDT 2016


On 2016-05-23 21:51, Lodovico Giaretta wrote:
> Hi,
>
> Today I stumbled upon this weird error:
>
>      struct ConfigContainer
>      {
>          Config[string] configs;
>      }
>
>      struct Config
>      {
>          string foo;
>          string bar;
>      }
>
>      enum ConfigContainer cc = {
>          configs: [            // error: not an associative array
> initializer
>              "MyConfig": {
>                  foo: "foo",
>                  bar: "bar"
>              }
>          ]
>      };
>
> But this other way works fine:
>
>      enum ConfigContainer cc = {
>          configs: [
>              "MyConfig": Config("foo", "bar")
>          ]
>      };
>
> Is this a bug? Or is this by design?

There's a limitation when using the static initialization syntax for 
structs. This works:

Config c = { foo: "foo", bar: "bar };

That works because the type is known at the left side. This will not work:

auto c = { foo: "foo", bar: "bar };

Because the compiler doesn't now the type of the struct literal. You can 
have to structs with different names but with the same members and 
types. The compiler is not smart enough to figure out the type in cases 
like function calls:

void foo(Config config);

foo({ foo: "foo", bar: "bar });

The above will not work because it can cause issues with function 
overloading and making the compiler even more complicated.

Your second example works because the type is tied to the 
initialization. What we need is something like this [1]:

auto c = Config{ foo: "foo", bar: "bar };

The compiler will know for sure that "c" is of type Config because the 
right side includes the type.

[1] https://issues.dlang.org/show_bug.cgi?id=15692

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list