July 1st, 2024

Zig-style generics are not well-suited for most languages

Zig-style generics, inspired by C++, are critiqued for limited universality. Zig's simplicity contrasts with Rust and Go's constraints. Metaprogramming praised for accessibility, but error messages and compiler support pose challenges. Limited type inference compared to Swift and Rust.

Read original articleLink Icon
Zig-style generics are not well-suited for most languages

Zig-style generics, resembling C++, are critiqued for not being universally suitable for all languages. While Zig aligns generics with its philosophy of simplicity, other languages may not share this view. The post highlights that Zig's approach, akin to templates, lacks upfront constraints in function signatures, contrasting with languages like Rust or Go. Zig's metaprogramming capabilities are praised for being more approachable than C++ due to its simplicity. However, the post warns against the complexity of error messages in generic code, a common issue in languages like C++. It also points out limitations in compiler support for both writing and calling generic code, emphasizing the need for extra work to ensure a good developer experience. Additionally, the post discusses the challenges posed by limited type inference in Zig-style generics compared to languages like Swift or Rust. Overall, the post suggests that while Zig's approach may work well for Zig itself, it may not be the best fit for most other languages due to various constraints and complexities involved.

Link Icon 3 comments
By @saghm - 5 months
> One meta point to keep in mind: many of the points here mirror the downsides of dynamic typing in comparison to static typing (again, not to imply that dynamic typing is not sometimes useful), because in some sense, it is the same problem but in a different context – instead of run time vs compile time, we’re talking about instantiation time vs declaration time. Yes, you have more flexibility to do whatever you want, but the problem is that you have more flexibility to do whatever you want.

This is the first time I've seen something said so similarly to how I feel about what the article describes as "template"-style generics; it has a similar user experience to duck typing where you have to just use something that fits the expected shape where it's being used or else everything falls apart. The happy path where everything is done correctly is certainly less verbose, but at least personally I don't end up feeling like it's less work in the long run to have the errors that inevitably happen when I slip up take so much more effort to debug.

By @jamincan - 5 months
>This post is meant to discuss reasons why Zig-style generics are not well-suited for languages other than Zig. Zig’s generics are in some sense well-suited to Zig, because they line up with the language’s philosophy that it is very important to have fewer concepts rather than more concepts.

>However, most languages don’t share that philosophy, at least, not as strongly as Zig does.

Bryan Cantrill talked about values of different programming languages [1] and I think it's a particularly useful way to compare different languages and the choices they make when developing them.

It often seems that criticism that is directed at a language is simply a reflection of a difference in values.

1. https://corecursive.com/024-software-as-a-reflection-of-valu...

By @dang - 5 months
Discussed at the time:

Zig-style generics are not well-suited for most languages - https://news.ycombinator.com/item?id=33142751 - Oct 2022 (128 comments)