zena:hashable

zena
import {…} from 'zena:hashable';

Interfaces

Hashable

zena
interface Hashable

A type that can be used as a key in hash-based collections like HashMap and HashSet.

The contract: if two values are equal (per ==, which is a virtual call to operator == when the class defines one, and reference equality otherwise), they must return the same hashCode. The reverse is not required — unequal values may share a hash code, though fewer collisions means better performance.

A class that uses reference equality (no operator ==) should return an identity hash: a value that is unique per instance and stable for the lifetime of the instance, e.g. a counter assigned in the constructor.

Case classes automatically satisfy Hashable: the compiler generates a structural hashCode() (and operator ==) from their parameters. Primitives (i32, boolean, enums, and distinct types over them) also satisfy the Hashable constraint; they hash to their own value. Strings hash with FNV-1a, cached after the first computation.

Methods
zena
hashCode(): i32
#

Functions

hash

zena
declare function hash<T>(value: T): i32

The hash code of a value, dispatched on its static type: primitives hash by value, Strings by content, and classes through hashCode().

This is what hash-based collections call. Prefer it over hashCode() — it works for primitives, which have no methods.

equals

zena
declare function equals<T>(a: T, b: T): boolean

Whether two values of the same type are equal, dispatched on their static type: primitives compare by value, Strings by content, and classes through operator == when the class defines one and by reference otherwise.