cannot initalize a struct for AA

Neia Neutuladh neia at ikeran.org
Sat Dec 15 02:33:27 UTC 2018


On Sat, 15 Dec 2018 02:22:51 +0000, Michelle Long wrote:
> AA[key] = {a,b,c};
> 
> fails.

AA[key] is not a variable initialization. Struct initializer literals are 
only valid at variable initialization. I think a few people (at least one, 
which is to say, me) have looked at adding a way to make them work at 
arbitrary locations. One thing that makes that not so easy is that the 
syntax doesn't include the type, so you'd need a way to explicitly specify 
it. For example:

---
struct Foo { int i; }
struct Bar { int i; }
struct HasProperty
{
  void property(Foo foo) {}
  void property(Bar bar) {}
}
HasProperty hp;
hp.property = {i: 10};
---

Which overload should that call? It's ambiguous.

An alternative would be to add reorderable, default-value-able named 
parameters and make compiler-generated struct constructors use them. 
That's a much more general solution and is therefore more likely to land 
in the language.


More information about the Digitalmars-d mailing list