Re: Struct literals and AA literals

2016-05-24 Thread Lodovico Giaretta via Digitalmars-d-learn

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




Re: Struct literals and AA literals

2016-05-24 Thread Jacob Carlborg via Digitalmars-d-learn

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


Struct literals and AA literals

2016-05-23 Thread Lodovico Giaretta via Digitalmars-d-learn

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