Implicit conversions for AA keys

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Mar 23 11:48:56 PDT 2012


On Fri, Mar 23, 2012 at 01:31:28PM -0500, Andrei Alexandrescu wrote:
[...]
> Let's see what requirements need to be satisfied by []. Say k is a
> value of the key type and x is a value being looked up.
> 
> First, we need to be able to evaluate k == x. So the types must be
> comparable.
> 
> Second, we need if x == k, then hash(k) == hash(x). This is tricky
> in general, but let's say we can approximate to the compile-time
> requirement that the hash function resolves to the same entity for
> both typeof(x) and typeof(k). This would rule out e.g. int and
> double but would leave char[] and string.

How do we check at compile time whether the hash function resolves to
the same entity?


> To include int and double correctly, we'd amend the second rule as
> follows. If typeof(x) converts implicitly to typeof(k), then use
> hash(cast(typeof(k)) x) instead of hash(x). This makes it possible
> to look up for an int in a hash of doubles, but not vice versa,
> which is great.

OK.


> These two are sufficient for lookup. For store, we also need the
> third rule, which is to!(typeof(k))(x) must compile and run.
[...]

Isn't this already required by the hash lookup? Or is casting different
from to!X, in which case it might be messy to import the relevant parts
of phobos into druntime. :-/


T

-- 
"A man's wife has more power over him than the state has." -- Ralph Emerson


More information about the Digitalmars-d mailing list