Parametricity is the property that a generic function's type signature alone can constrain—or even fully determine—its implementation. In Rust, a function `fn mystery<T>(a: T) -> T` can only return its argument; the type system enforces this. Zig's `comptime`, by contrast, allows the function body to inspect and branch on the type at compile time, breaking parametricity entirely. While comptime is genuinely powerful for staging (running arbitrary code at compile time), it conflates staging with generic programming. Languages like Rust and Haskell solve ad hoc polymorphism through traits and type classes, which preserve parametricity while still allowing type-specific behavior. The author argues that comptime is a worse fit for generic programming specifically, because it sacrifices the reasoning guarantees and comprehension benefits that parametricity provides—benefits that compound across an entire codebase.

8m read timeFrom noelwelsh.com
Post cover image
Table of contents
ParametricityThe Cost of Understanding CodeChoices at Compile TimeConclusions

Sort: