We have traits at home

This article explores the trade-offs between using interfaces and comptime duck typing for polymorphism in the Zig programming language. It argues that while interfaces provide standard contracts, comptime offers superior performance for critical code paths.
Why it matters
It provides insights into language design and performance optimization for developers working with low-level systems programming.
I have had more time recently to write code in Zig [1] , and it has been good for my soul. I wanted to share some insights I have arrived at regarding abstractions in Zig, including some techniques that I have found useful but not seen employed more broadly.
Considerable [2] ink [3] has [4] been [5] spilled [6] on Zig Interfaces, and justifiably so. While not a language feature, interfaces are the de-facto way of implementing runtime polymorphism in Zig. Yet [7] more [8] has been said about zig's comptime, and how it may be leveraged to avoid the performance woes of dynamic dispatch and write slick, data-oriented code.
To put it briefly, polymorphism in Zig is conventionally obtained in one of two ways:
This state of affairs has proven to be quite effective so far, but it is not without its tradeoffs.
Get smarter about the news
Sign up free for a feed built around what you actually care about, Dive Deeper research on any story, and the full text of every article.
Create free accountAlready have an account? Sign in