Why does struct initializer works for arrays but not for associative arrays?
Andre Pany
andre at s-e-a-p.de
Wed Mar 14 16:53:09 UTC 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é
More information about the Digitalmars-d-learn
mailing list