www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct literals and AA literals

reply Lodovico Giaretta <lodovico giaretart.net> writes:
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?

Thank you in advance.

Lodovico Giaretta
May 23 2016
parent reply Jacob Carlborg <doob me.com> writes:
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
May 23 2016
parent Lodovico Giaretta <lodovico giaretart.net> writes:
On Tuesday, 24 May 2016 at 06:59:18 UTC, Jacob Carlborg wrote:
 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
It would indeed be very handy to have this syntax. Thank you for creating the enhancement request. Lodovico Giaretta
May 24 2016