Hacker News new | past | comments | ask | show | jobs | submit login

What necessary use case of macros isn’t addressed by comptime in zig?



zig 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


I 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.


Well, 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


I can't really judge, I've barely tried Zig but... Maybe X-macros? Maybe compiler-specific attributes? Maybe reducing some syntactic clutter?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: