Separating a Hash type from Object
Graydon Hoare
graydon at mozilla.com
Wed May 30 17:50:56 PDT 2007
Jason Orendorff wrote:
> OK, it strikes me as a little weird, now, to use intrinsic::=== and
> intrinsic::hashcode() here.
>
> Java, Python, and Ruby let individual *classes* (rather than individual
> hash tables) override hash() and equals(). I think this supports
> generic code better.
Not all objects in ES4 are of an interesting class, or implement an
interesting interface. Think structural types: the nearest class of the
type "{a:int}" is "Object".
Still, it's pretty easy to have both:
function defaultHash.<K>(k:K) : uint {
switch type (k) {
case (h:Hashable) {
return h.hash();
}
default {
intrinsic::hashcode(k);
}
}
}
function defaultEq.<K>(a:K, b:K) : bool {
switch type (k) {
case (h:Equality) {
return a.equals(b);
}
default {
return a === b;
}
}
}
class Dict.<K,V> {
function Dict(hash=defaultHash.<K>, eq=defaultEq.<K>)
{
...
}
...
}
-graydon
More information about the Es4-discuss
mailing list