Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

DefaultKeyHash

Hashes a supported built-in key using logical contents, without allocation.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.collections

import std.collections;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

DefaultKeyHash

pub fn DefaultKeyHash<K>(Key: readonly K&) -> u64 where K: BuiltinHashKey

Hashes a supported built-in key using logical contents, without allocation.

Integer bits and ordinal UTF-8 bytes determine hashes; padding and owner addresses never participate. Signed floating zero hashes equally and all NaNs identify one equivalent key. Hashes are deterministic noncryptographic indexing values, not stable persisted identifiers or protection against hostile collisions. Readonly inputs are borrowed only for this call; concurrent immutable access is supported. Unsupported custom types require an explicit HashKeyPolicy. K: Supported built-in scalar, str or immutable String key representation. Key: Readonly key, never retained. Return: Value hash consistent with DefaultKeyEquals for the same key type.

Example

module example.default_hash;
from std.collections import DefaultKeyHash, DefaultKeyEquals, CreateHashMap, CreateHashSet;
fn Main() -> i32 {
    let Positive = 0_f64;
    let Negative = bit_cast<f64>(0x8000000000000000_u64);
    let Nan = bit_cast<f64>(0x7FF8000000000001_u64);
    if !DefaultKeyEquals<f64>(Positive, Negative) { return 1; }
    if DefaultKeyHash<f64>(Positive) != DefaultKeyHash<f64>(Negative) { return 2; }
    if !DefaultKeyEquals<f64>(Nan, Nan) { return 3; }
    let Lambda = 'λ';
    let Letter = 'A';
    if !DefaultKeyEquals<char>(Lambda, Lambda) || DefaultKeyEquals<char>(Lambda, Letter) { return 8; }
    if DefaultKeyHash<char>(Lambda) != DefaultKeyHash<char>(Lambda) { return 9; }
    if DefaultKeyHash<char>(Lambda) == DefaultKeyHash<char>(Letter) { return 10; }
    var Values = CreateHashMap<str, i32>();
    if Values.TryInsert("answer", 42) is .Err { Error = _ } { return 4; }
    if Values.Find("answer").Unwrap() != 42 { return 5; }
    var Keys = CreateHashSet<i128>();
    if Keys.TryInsert(-1_i128) is .Err { Error = _ } { return 6; }
    if !Keys.Contains(-1_i128) { return 7; }
    return 0;
}

DefaultKeyEquals

pub fn DefaultKeyEquals<K>(Left: readonly K&, Right: readonly K&) -> bool where K: BuiltinHashKey

Compares supported built-in keys using the default map equivalence relation.

Integer/boolean/character keys use exact value equality; text uses ordinal UTF-8 bytes. Floating signed zeros are equivalent and every NaN belongs to one key equivalence class. This policy does not change floating language operators. The operation allocates nothing and retains neither readonly input. K: Supported built-in key representation. Left: First readonly key borrowed only for this call. Right: Second readonly key borrowed only for this call. Return: Whether both keys identify the same logical table entry.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library