Implicit conversions for AA keys

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Mar 23 12:06:15 PDT 2012


On 3/23/12 1:48 PM, H. S. Teoh wrote:
> How do we check at compile time whether the hash function resolves to
> the same entity?

int fun(T)(T x) {
     return 42;
}

void main() {
     static assert(&fun!int != &fun!double);
}

This actually reveals a compiler bug:

Assertion failed: (d->purity != PUREfwdref), function typeMerge, file 
cast.c, line 1909.

A cast would be needed anyway because they have different types, too. 
Anyway upon more thinking maybe this is too restrictive a rule. It won't 
catch e.g. functions that are, in fact, identical, but come from 
distinct instantiations.

So perhaps you need some conservative approximation, i.e. look if the 
two types are qualified versions of the same type and then assume they 
hash the same.

>> 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. :-/

Casting is very different from to, and useless for your purposes. You 
must use to.


Andrei


More information about the Digitalmars-d mailing list