4.8 Generics
fn max<T>(a: T, b: T) -> T where T: Ord => if a > b { a } else { b }
data Page<T> where T: Serializable { items: List<T>; next: Cursor? }
data Matrix<const R: usize, const C: usize> { cells: [f64; R * C] }
fn pack<T>(x: T) -> bytes where T: Serializable, size_of(T) <= 4KiB
data Cache<K: Hash + Eq, V = bytes> { … }- Parameters: type parameters
T, constant parametersconst N: Type(primitives,str, enums without fields). - Constraints: interfaces (
T: A + B) and comptime predicates. - The implementation MUST monomorphise generics;
dyn Iis the only way of type erasure. - Generics are invariant. Arguments are inferred from the call arguments and the expected type; if ambiguous — an error.
- There is no specialisation.
Related diagnostics
bound-not-satisfied, generic-args
See also
Source: docs/spec/oxl-standard.en.md