3 ms·
The thing that makes Zig comptime radically different is that types are comptime values, so you can make functions that take types as arguments and return new t
by int_19h 4y ago
The thing that makes Zig comptime radically different is that types are comptime values, so you can make functions that take types as arguments and return new types as results - it's like C++ templates on steroids, yet conceptually simpler.
- WalterBright 4y agoWell, D does, too: template MakePtrType(T) { alias MakePtrType = T*; } MakePtrType!int p; pragma(msg, "p is type ", typeof(p)); dmd -c test2.d p is type int* or perhaps I am misunderstanding you?
- FeepingCreature 4y agoTo be fair, now we're well out of the realm of CTFE. (Though that edges into the macros debate.)
- Jensson 4y agoYou aren't returning the new type there, looks like you just inline that code. Zig lets you create and return new types that way while your example looks like just regular template substitution. Edit: So the main difference seems to be that types are first class citizens in Zig, you can write a normal comptime function that returns a type and either use it to do more metaprogramming or everywhere you would declare a type for a function or variable. I am not aware of any static typed languages that lets you do this with normal language syntax. C++ does it with templates, but in Zig you can do what C++ templates can but with if statements and loops.
- pjmlp 4y agoJust use the Circle compiler and you have the full C++ language at compile time, available today.
- WalterBright 4y agoIt is returning a new type. Please show me an example where it differs. D's metaprogramming is very good at constructing types. A member of the D community tried very hard to implement type functions, but it turned out to be redundant with D's existing capability.
- pjmlp 4y agoIf anything it is only a simpler syntax for C++ template meta-programming, not needed knowledge about SFINAE and tag dispatching tricks, most of which are anyway no longer required with the improvements made across C++17, 20 and 23.
- mejutoco 4y agoIt reminds me of Idris, where the types can actually run programs (at compile time) as long as they can be proved to terminate, which is more often than I though.