News for hackers. Stuff that matters.
Re:Zig's Comptime Is Bonkers Good by kreco on Tuesday January 07, 2025 @09:27AM (#42622636)
> Cool, you now invented your own DSL and half-baked meta programming macro language

I'm not sure I'm following your statement. What he said was to use a C program to parse C code and emit additional C code. There is no mention of DSL.

> for something that shall have been in the language to begin with.

The whole point of this discussion is to debate on that.

I have no strong opinion (yet) but the meta program looks easy to understand compared to the pandora box of metaprogrammaing withing the language (since it requires standardization, limitations etc.).

Re:Zig's Comptime Is Bonkers Good by ezwoodland on Tuesday January 07, 2025 @02:11PM (#42626107)
I think the parsed c program is the DSL. You have to write a parser and compiler for the c-like source to the actual c code.
Re:Zig's Comptime Is Bonkers Good by adev_ on Tuesday January 07, 2025 @04:06PM (#42627458)
> What he said was to use a C program to parse C code and emit additional C code. There is no mention of DSL

Because it is always the same story:

- You start by writing a little meta-compiler to solve one specific codegen problem in a specific portion your code.

- Then you realize their is many slight variations of this problem in other areas of your project or other projects... because it is exactly what *Genericity* is all about. And we know that since literally the 1970s and freaking LISP.

- To avoid your meta-compiler to become a Frankenstein of options with endless hardcoded logic, you makes it interpret some annotations in C comments, some preprocessors or some template files somewhere.

- Congrats, you completed the loop and invented your own half baked DSL.

I have seen that many time, in many places. Again and again. Often because there is a category of C programmers that would prefer to swim in their own shit instead of using few C++ templates.

Codegen is consistently a terrible solution to a well studied problem: meta-programming. The fear of the feature creep (templates, macros) shall never be a justification to create some half baked complexity monster that will alaways finish worst than the problem they try to avoid.

If you doubt about that: Just use a lexer or parser generator. Or better, the quintessence of codegen: Autotools. They are a perfect illustration of how terrible and how fucking unmaintainable Codegen is.

I can think of a single use case where a meta-compiler and a DSL are appropriate solutions: *Serialization* (Protobuf, Thrift, CapNProto, ...). Because in this precise case, you actually do want a language neutral way to express your interface: you want a DSL (IDL).

Currently here, Zig does the right thing: Comptime execution for meta-programming is one order of magnitude better than anything available in C or C++ before C++20