Hashable
public protocol HashableProtocol for types whose values can be hashed.
Hashable extends Equatable: the contract is that a == b implies
a.hash(into:) and b.hash(into:) feed the same bytes to the hasher.
Violating this breaks Set and Dictionary — equal lookups won't
land on the equal stored value. The hasher is generic so the same
hash impl works across hashing algorithms (SipHash, FxHash, etc.).
Examples
public struct Tag: Hashable {
public var name: String
public func isEqual(to other: Tag) -> Bool { self.name == other.name }
public func hash[H](mutating into hasher: H) where H: Hasher {
self.name.hash(into: hasher)
}
}Methods
func hash[H](into: mutating H) where H: Hasher
func hash[H](into: mutating H) where H: HasherFeeds this value's bytes into hasher. Must be deterministic
across calls and consistent with isEqual.
ImplementsEquatable
Associated Types
type Output = Bool
type Output = BoolMethods
public func equal(to: Self) -> Bool
public func equal(to: Self) -> BoolBridges Equal.equal(to:) to Equatable.isEqual(to:).
func isEqual(to: Self) -> Bool
func isEqual(to: Self) -> BoolReturns true iff self and other are considered equal. Should
be reflexive, symmetric, and transitive — Hashable requires equal
values to hash equal, so don't drift from those laws.
public func notEqual(to: Self) -> Bool
public func notEqual(to: Self) -> BoolDefault !=: delegates to == so there's a single source of truth.
Defined in lang/std/core/protocols.ks