4 ms·
What necessary use case of macros isn’t addressed by comptime in zig?
by BaculumMeumEst 5y ago
What necessary use case of macros isn’t addressed by comptime in zig?
- jstimpfle 5y agoI can't really judge, I've barely tried Zig but... Maybe X-macros? Maybe compiler-specific attributes? Maybe reducing some syntactic clutter?
- FrozenVoid 5y agozig comptime is the same as constexpr/consteval in C++ which operate on variables and valid code blocks. C macros operate on arbitrary tokens, which get converted to code or constants. For example variadic functions can be implemented to act on token arglists that act as abstract tuples: https://github.com/FrozenVoid/C-headers/blob/main/argmanip.h https://github.com/FrozenVoid/C-headers/blob/main/argmanip.h
- anonymoushn 5y agoI see. The parts of this header file that cannot be trivially ported to Zig are those that involve conditional evaluation at runtime. You can use comptime to include this sort of sub-language in the language (and this is how e.g. std.fmt works), but I'm not sure if one can achieve the level of tight coupling with the outer language that you have. It's an explicit goal of the language that you can tell what the local control flow is by reading the code, but of course one is free to run the C preprocessor as part of the build if one disagrees. I don't think C++ constexpr/consteval can be used to write things like generic json serializers and deserializers, which Zig does in the standard library using comptime, but I'm not sure.
- throw10920 5y agoWell, to the point of the submissions - Hare doesn't have anything comparable to C macros, zig comptime, or Lisp macros (and probably never will[1]), so it doesn't matter what flavor of metaprogramming you want - you're not going to get it. [1] https://news.ycombinator.com/item?id=31152272 https://news.ycombinator.com/item?id=31152272