We have traits at home
Web page · https://axelmagn.com/blog/202610-zig-traits
First seen by Oriel on .
AI summary
The article proposes compile-time Traits as Zig structs bundling functions and types, providing explicit, monomorphized contracts that sit between runtime interfaces and comptime duck typing.
- A `Num(T)` trait packages arithmetic functions so generic code such as a Möbius transformation receives its operations explicitly without runtime dispatch.
- Trait implementations can be built from anonymous structs with `impl` or retrieved from a type’s declarations with `getTrait`.
- The pattern works with primitive types through generated implementations and lets the compiler inline and monomorphize trait calls.
- Traits require the operated-on type to be known in advance, making associated types and type erasure awkward.
- The author presents traits as a userland convention inspired by Rust traits, Haskell typeclasses, and OCaml modules, requiring no library machinery.
#zig #compile-time programming #type systems #polymorphism
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Points
Score reported by the source.
- Hacker News: 53, latest observation ; +45 over observed period since
- Lobsters: 53, latest observation ; +51 over observed period since
Comments
Comment count reported by the source.
- Hacker News: 18, latest observation ; +18 over observed period since
- Lobsters: 13, latest observation ; +13 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Hacker News | 50202482 | https://www.axelmagn.com/blog/202610-zig-traits/ Read discussion |
| Lobsters | 05xrj8 | https://www.axelmagn.com/blog/202610-zig-traits/ Read discussion |