4 ms·
By #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 ex
by haberman 4y ago
By #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.