Hashable Interface
🚧 Status: The
Hashable
interface is not implemented yet.
A hashable type is a type that can be hashed to an integer hash value, i.e., it is distilled into a value that is used as evidence of inequality. Types are hashable when they implement the Hashable
interface.
Hashable types can be used as keys in dictionaries.
Hashable types must also be equatable, i.e., they must also implement the Equatable
interface. This is because the hash value is only evidence for inequality: two values that have different hash values are guaranteed to be unequal. However, if the hash values of two values are the same, then the two values could still be unequal and just happen to hash to the same hash value. In that case equality still needs to be determined through an equality check. Without Equatable
, values could be added to a dictionary, but it would not be possible to retrieve them.
Most of the built-in types are hashable, like booleans and integers. Arrays are hashable when their elements are hashable. Dictionaries are hashable when their values are equatable.
Hashing a value means passing its essential components into a hash function. Essential components are those that are used in the type's implementation of Equatable
.
If two values are equal because their equals
function returns true, then the implementation must return the same integer hash value for each of the two values.
The implementation must also consistently return the same integer hash value during the execution of the program when the essential components have not changed. The integer hash value must not necessarily be the same across multiple executions.
Last updated