3 ms·
The killer feature of Zig IMO is comptime. C++ has spent over a decade now marching towards making more and more of the language available at compile time, but
by haberman 3y ago
The killer feature of Zig IMO is comptime. C++ has spent over a decade now marching towards making more and more of the language available at compile time, but in an awkward and complicated way (constexpr, consteval, constinit). Zig comes out of the gate with a unified and coherent compile-time evaluation feature that effectively obsoletes not only constexpr/consteval/constinit, but C++ templates too. This is "doing more with less" in a way that I find really compelling.
- Conscat 3y agoZig does not have anything analogous to constinit, because Zig does not have object lifetimes or constructors. Comptime is also a massive pain to use for generating new data types (you have to return a type from a comptime function), and it cannot express anything analogous to CRTP.
- haberman 3y agoI personally find returning a type from a comptime function to be quite elegant. Template parameters and regular parameters are unified into a single concept. Zig's comptime is analogous to constinit, because it lets you compute a value at compile time and ensure that it is linker-initialized into the final binary image. The point about CTRP is interesting, I'd have to think about that some more.
- Conscat 3y agoI would consider that similar to a `inline constexpr` variable rather than a `constinit` variable. A `constinit` variable calls a constexpr constructor, but doesn't produce a constant. EDIT: Oh, I think I see how it's like `constinit`. If you have a non-`comptime` variable that is initialized by a `comptime` function, then it's a non-constant constant-initialized variable.