Why 'Zero-Cost' Value Classes Still Need Compiler Help
Value classes are a popular language feature meant to let developers wrap primitive types (like an Int or a String) in more meaningful, type-safe names without paying a performance penalty at runtime. Languages like Kotlin, Scala, and Swift all offer some version of this.
A recent blog post making the rounds on Hacker News argues that this "zero-cost abstraction" promise is often more marketing than reality. Without deliberate compiler support - inlining, unboxing, and careful handling of generics - value classes frequently end up allocating extra objects or adding indirection anyway, defeating their purpose. The author walks through concrete cases where naive implementations leak overhead, and argues that language designers need to treat value classes as a first-class compiler concern rather than a syntactic sugar afterthought.
The discussion resonated with developers who've hit surprising boxing behavior in production code.