You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we make the incoming hash argument a keyword instead of positional it would have a few benefits:
A common mistake when defining custom hashing is to define hash(x::MyType) instead of hash(x::MyType, h::UInt). This leads to broken behavior with no error. If the initial hash value was supplied by generic code using the hash function as a keyword argument, this mistake would get a clear error message that hash(x::MyType) does not accept an h keyword argument, which seems like it would make it much easier to figure out what's wrong.
It would open up the hash API to allow a varargs version that chain hashes its arguments: hash(arg1, args...; h::UInt=zero(UInt)) would be defined like this:
functionhash(args...; h::UInt=zero(UInt))
for arg in args
h =hash(arg, h=h)
endreturn h
end
Ideally, the common pattern of generating a random value for each type and explicitly using it in custom hashing functions could be avoided and replaced with a compile-time call to hashing a type name or something like that. I would like to define a custom hashing function like this:
Base.hash(x::MyType; h::UInt=zero(Uint)) =hash(h = h +hash(MyType), x.f1, x.f2, x.f4)
Note: a single call to hash does chaining for you; the call to hash(MyType) is constant could be evaluated at compile time – if we base hash(MyType) on a canonical name of MyType then it would be stable across Julia processes.
The text was updated successfully, but these errors were encountered:
If we make the incoming hash argument a keyword instead of positional it would have a few benefits:
A common mistake when defining custom hashing is to define
hash(x::MyType)
instead ofhash(x::MyType, h::UInt)
. This leads to broken behavior with no error. If the initial hash value was supplied by generic code using thehash
function as a keyword argument, this mistake would get a clear error message thathash(x::MyType)
does not accept anh
keyword argument, which seems like it would make it much easier to figure out what's wrong.It would open up the
hash
API to allow a varargs version that chain hashes its arguments:hash(arg1, args...; h::UInt=zero(UInt))
would be defined like this:Note: a single call to
hash
does chaining for you; the call tohash(MyType)
is constant could be evaluated at compile time – if we basehash(MyType)
on a canonical name ofMyType
then it would be stable across Julia processes.The text was updated successfully, but these errors were encountered: