# Zig Has No Traits, So This Developer Built Them in Userland

Original title: We have traits at home

> Hacker News story 50202482 · language: en · submitted 2026-10-22
> Web version: https://zeli.app/story/50202482
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 53 · by [Ygg2](<https://news.ycombinator.com/user?id=Ygg2>) · 💬 18
[original](<https://www.axelmagn.com/blog/202610-zig-traits/>) · [HN discussion](<https://news.ycombinator.com/item?id=50202482>)

## Summary

Zig offers two paths to polymorphism: runtime interfaces with vtables and dynamic dispatch, or comptime duck typing with monomorphization. The first gives type contracts but blocks compiler optimization; the second is fast but loosely specified. Axel Magnus proposes a third convention: Traits as comptime structs holding related functions over a type, inspired by OCaml. Passing a trait struct into a function gives a clear contract, keeps LSP autocompletion working, and lets the compiler inline and unroll loops with no library machinery required.

## Key quote

> Traits are just bags of functions and types that tell the receiving function how to execute subtasks that it's not responsible for defining. They behave a lot like vtables, except that they are evaluated at comptime rather than runtime.

## HN discussion (18 comments)

### [detourdog](<https://news.ycombinator.com/item?id=50212779>)

> First I had heard of duck-typing.
>
> https://en.wikipedia.org/wiki/Duck_typing
>
> After reading the page I was left with the question of isn't that dynamic inheritance? The seems like dynamic runtime.

### [Panzerschrek](<https://news.ycombinator.com/item?id=50206092>)

> I see little to no benefits of introducing such "traits" into a language having duck-typing in templates. It may sometimes improve code readability (slightly), but with more boilerplate code for traits handling in exchange. And the result machine code will be identical to code with no traits at all (with only duck-typing).
>
> C++ is a good example of this. It had duck-typing in templates for decades. Then some sort of trait-like mechanism named concepts has been introduced. The only benefit of them seems to be slightly better error messages if someone misuses a template class/function.

### [no1guangming](<https://news.ycombinator.com/item?id=50205909>)

> Can't load the page

---

## Cite

- Page: https://zeli.app/story/50202482
- Date: 2026-10-22
- Source: axelmagn.com (https://www.axelmagn.com/blog/202610-zig-traits/)
- Hacker News: https://news.ycombinator.com/item?id=50202482

## Other languages

- zh: https://zeli.app/zh/story/50202482 · markdown: https://zeli.app/zh/story/50202482.md
- ja: https://zeli.app/ja/story/50202482 · markdown: https://zeli.app/ja/story/50202482.md
- ko: https://zeli.app/ko/story/50202482 · markdown: https://zeli.app/ko/story/50202482.md
- de: https://zeli.app/de/story/50202482 · markdown: https://zeli.app/de/story/50202482.md
- es: https://zeli.app/es/story/50202482 · markdown: https://zeli.app/es/story/50202482.md
- ru: https://zeli.app/ru/story/50202482 · markdown: https://zeli.app/ru/story/50202482.md
