Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Lack of macros in recent systems languages like Zig/Hare/etc seems to be a design flaw, a handicap marketed as safety feature or bargain for clarity. If a language can't replace C macros, it cannot replace C.


C doesn't exactly have macros. That's a text substitution done by the preprocessor in its own limited, special-purpose language. True hygienic macros of something like Common Lisp are a different beast.

If all you want are textual substitutions, you can use the C preprocessor in front of any language.


C macros are as abstract or even more abstract than hygienic macros https://github.com/BlueFlo0d/CSP https://github.com/FrozenVoid/C-headers/blob/main/argmanip.h


> you can use the C preprocessor in front of any language.

You will be paddling upriver if that language doesn't have a mostly C-compatible token structure, or has white space sensitivities that C preprocessing doesn't preserve and such.


The C Preprocessor has sensitivities that aren't matched by C in all cases. And in the case of Hare, the tokenisation should be close enough to C that the CPP would only break in the same ways it already does.


Common Lisp does have macros, but not hygienic macros.


Thanks for the correction.


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?


Could you expand on this? I'm really curious why you think so. I have written and worked with C codebases that do quite well without using macros at all.


It is a general capability to modify C code at will, example: https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html


It could be portability stuff, like compiler-dependent attributes.

It could be fixing shortcomings of the languages without a huge complicated system that needs to be built into the language (and if it isn't, you can't just switch languages).

It could be dirty hacks that abstract a problem at the syntactic level (this is the kind of macro that is most likely to be replaced).


You might want to learn more about Zig's comptime.


I don't think many Zig programmers miss C macros. Comptime is extremely powerful.


C macros are powerful enough to create entire functional languages at compile time, which is far beyond any constexpr functions. https://github.com/rofl0r/order-pp


If it's at compilation time, you have the full zig language so you certainly could implement your very own functional programming language https://github.com/igmanthony/zig_comptime_lisp in regular zig rather in an external preprocessor.


I do think comptime is worth a look for you, even if my comment elsewhere in this thread admits that it can't quite do everything you do with C macros.




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

Search: