top | item 43446785

(no title)

Wumpnot | 11 months ago

It just looks like C++ templates with a slightly different syntax ..

discuss

order

azakai|11 months ago

Exactly, the point is that C++ added templates as a huge new language feature, while in Zig it is just one of the things that is immediately possible thanks to comptime.

throwawaymaths|11 months ago

well, not quite since you can pass non-type things of generally any level of data type complexity (as long as it's comptime-valid, which only excludes certain types of mutation), and do stuff with them that you couldnt in c++.

    fn MyType(T: type, comptime tag: [] u8, comptime count: usize) type {
      const capitalized = some_module.capitalize(tag);
      return struct{
        fn name() []const u8 {
          return capitalized;
        }
        array: [count]T,
      };
    }
for example

Wumpnot|11 months ago

That example looks easy enough to replicate in C++ with consteval + template, basically the same except a few minor syntax changes.