[Issue 2954] [tdpl] Allow to set associative array key value only using expression AA key type is constructable from

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Aug 10 12:43:41 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=2954

--- Comment #12 from Andrei Alexandrescu <andrei at erdani.com> ---
(In reply to hsteoh from comment #11)
> Which of the following cases should/shouldn't be valid?
> 
> Case 1:
> 
> int[string] aa;
> char[] key;
> aa[key] = 1;

This shouldn't compile, although we ought to make it work in the future (e.g.
by defining and using a protocol). The problem with this is efficiency:

int[string] aa;
char[] key = ...;
aa[key.idup] = 1;
// Alternatively
aa[to!string(key)] = 1;

This is correct but less efficient than it could - it compulsively allocates a
new string even when not needed (i.e. the key is already there).

> Case 2:
> 
> int[string] aa;
> char[] key;
> if (aa[key] == 1) { ... }
> auto p = key in aa;
> 
> 
> It seems to me that we should allow case 2, but prohibit case 1.

Yah, lookup should always work with any type that allows comparison.

--


More information about the Digitalmars-d-bugs mailing list