CEP-0016: Generic Parameter Inference¶
Draft
Draft proposal for inferring explicit comptime type parameters from ordinary arguments. V1 requires these parameters to be spelled unless another accepted rule supplies them.
Summary¶
Catalyst should eventually infer some explicit comptime type parameters from runtime argument types, such as inferring T from value: T, ptr: *T, or items: []T.
V1 keeps generic calls explicit so semantic instances, diagnostics, reflection, and prelude helper APIs remain predictable.
Example¶
Generic parameter inference would remove repetitive type arguments in common prelude and standard-library APIs. V1 shape:
alloc.destroy(Buffer, ptr)
alloc.free(u8, items)
box(T, alloc, value)
could become:
alloc.destroy(ptr)
alloc.free(items)
box(alloc, value)
Self-referential contract constraints create another common pressure point. When a generic type parameter's constraint mentions the parameter itself, the V1 signature can express the constraint directly, but calls still spell the type argument explicitly:
fn double(comptime T: Add(T, T), value: T) T {
return value + value
}
const x = double(i32, n)
If generic parameter inference is accepted, the future call could infer T from value: T while still checking the self-referential Add(T, T) constraint after T is known:
const x = double(n)
The constraint itself is not an inference source until the participating type parameter has a candidate. It validates the inferred type and supplies operator facts for the function body.
Motivation¶
The feature needs a precise inference model so omitted type parameters are not guessed from unrelated context or hidden runtime behavior.
Proposed Direction¶
The proposal should cover:
- inferring type parameters from exact value, pointer, slice, array, optional, and owner shapes;
- whether expected return type may contribute to inference;
- how inference works with default parameters;
- interaction with CEP-0004: Function Overloading;
- interaction with CEP-0015: Named Arguments;
- diagnostics for underconstrained, overconstrained, and ambiguous inference;
- reflection and documentation display for inferred
comptimearguments.
Inference should not make runtime values flow into comptime. It only recovers semantic type arguments from already-known static types.
V1 Compatibility¶
V1 spells typed helper type arguments explicitly. APIs that rely on inference must keep V1-compatible explicit forms until this proposal is accepted.