This is basically how you would do it in C as well.
I haven't had as much time as I would have liked to play around with Zig. But, it seems like Zig is like C, but with much stronger static checks, much less UB, lots of useful syntactic sugar, an anti-global agenda (explicitly passed allocator in Zig vs strtok in C) etc. And, truthfully, I really like this.
If we can move from a C/C++/Java world to a Zig/Rust/Go world, I would view that as an absolute win.
And the anytype stuff seems pretty close to C++ templates. I wonder how different comptime really is as compared to templates + constexpr?
As for C++, my experience is that templates can help produce some things in a type safe way that you later tend to regret and understand you should have never produced in the first place :-)
Just created some custom closure producing code that takes a pointer to any function along with arguments. The arguments get stored in custom allocations and can be applied to the function at any later point. Creating the template code is quite painful and probably none of my teammates will be able to maintain it. On the flipside, closures like this might be the right API for the use case.
Comptime is pretty much C++ templates without the need for a completely separate language for metaprogramming, which is the way it should be IMO.
(The downside is that there's no template parameter inference, but I'm pretty sure that this is one of those things that Zig would avoid by design anyway.)
I haven't had as much time as I would have liked to play around with Zig. But, it seems like Zig is like C, but with much stronger static checks, much less UB, lots of useful syntactic sugar, an anti-global agenda (explicitly passed allocator in Zig vs strtok in C) etc. And, truthfully, I really like this.
If we can move from a C/C++/Java world to a Zig/Rust/Go world, I would view that as an absolute win.