3 ms·
I feel that this post conflates two different things: 1. The model for expressing and instantiating a generic function. 2. The model for constraining
by haberman 4y ago
I feel that this post conflates two different things:
1. The model for expressing and instantiating a generic function.
2. The model for constraining a generic function such that type errors are as
local and comprehensible as possible.
Zig currently doesn't do #2 at all, true. Neither did C++ until C++20. Doing #2 clearly has benefits, as the article effectively argues. Those benefits can be realized later by adding constraints later, like C++ did.
When people argue that Zig made the right decision for generics, and that other languages should follow suit, they are talking about #1, not #2. To convince me that "Zig-style generics are not well-suited for most languages", you would need to convince me that Zig's choice for #1 will make it difficult or impossible to do #2 later.
- akkartik 4y agoI don't know what you mean by #1. Do you mean the syntax of putting `comptime` args into the arg list along with the regular ones?
- haberman 4y agoBy #1 I mean Zig's design of expressing all generic-related logic using normal control flow constructs that just happen to be evaluated at compile time. For example, suppose you want a generic function or generic type to have some special cases for certain types. In C++, you would write a template specialization, which is a special syntax. In Zig you would just write an if() statement: https://ikrima.dev/dev-notes/zig/zig-metaprogramming/#dynamic-specialization https://ikrima.dev/dev-notes/zig/zig-metaprogramming/#dynami...
- varajelle 4y agoFrom c++17, You can also use a if statement in c++ (if constexpr (std::is_same_v<T, int>) {...})
- haberman 4y agoYou can't use "if constexpr" to vary the type signature, for example (as in my link). Or to choose a different representation for a type based on a type parameter. Those will require metaprogramming at the template level. Another example: you can't write std::is_same_v itself using "if constexpr".
- AaronFriel 4y agoI think that what the author is suggesting is that you still have an open world problem that causes #2 to rear its head eventually, due to #1. You can try to use plain syntax to narrow types, but the only way to head off the gnarly errors is to close the universe of type parameters. That turns out to be a pretty weak form of generics.
- haberman 4y agoI don't think you can call Zig's choice for #1 the cause of the problems in #2. C++ made a different choice for #1 and yet still suffered the problems of #2 until C++20.