Re:Zig-style generics are not well-suited for most languages
by throwawaymaths on Sunday October 09, 2022 @07:51PM
(#33146418)
I think this could be basically done as a check in the stdlib relatively easily, via something like:
const MyConcept = std.concept.build(.{...});
const requires = std.concept.requires;
const MyType =
requires(
MyConcept,
struct{
...
});
requires simply passes through the type passed to it.If it fails, it gives some nicely formatted compile time errors.
Re:Zig-style generics are not well-suited for most languages
by kassane on Monday October 10, 2022 @09:02AM
(#33150820)
There are some projects trying to bring this experience.
https://github.com/ibokuri/concepts
https://github.com/mov-rax/zig-validate
https://github.com/yrashk/zig-ctc
Templates in C++ are quite different because of overload candidate resolution (and so SFINAE), which makes debugging templates a pain. Zig doesn't have overloads at all, plus, the error reporting mechanism (which C++ doesn't have) is available at compile time, as it is at runtime.
Having said that, I actually would like to see "concepts" in Zig, probably as a simple boolean predicate as the type, for one reason, and one reason only, which isn't mentioned in the article: documentation.