4 ms·
> This is the polar opposite of Zig’s embracing of metaprogramming for as much as possible. I found this claim a bit strange. do people actually use metaprogr
by throwawaymaths 1y ago
> This is the polar opposite of Zig’s embracing of metaprogramming for as much as possible.
I found this claim a bit strange. do people actually use metaprogramming in Zig a lot?
- Cieric 1y agoI can't comment about everyone, but I use it a lot for building libraries and such. I just recently built a database ORM, but since their metaprogramming is used for their generics I've run across it a lot just for those case.
- jamiejquinn 1y agoIMO it's one of Zig's advantages over C (text substitution macros are both too powerful and not powerful enough). If you're using Zig without metaprogramming, you're leaving a useful (if advanced) tool on the table.
- johnnyjeans 1y agoC's preprocessor not being powerful enough is less a problem with text-substitution macros, more that they're not allowed to expand recursively which was an intentional design choice. There are some hacks you can do to get around it, and people have done cool things[1] within the limited space those hacks grant you, but there's a reason many C projects with extensive metaprogramming just use an external macro language (which is also just text-substitution). IMO AST Macros are an even bigger problem than text substitution. Debugging metaprogramming of any kind past a certain point of complexity is a royal pain in the ass (even in a Lisp, Forth or Prolog), but AST explorers are next to useless for complicated logic problems that involve needle in a haystack searches of side effects. If you're dealing with a large AoS of heterogeneous SoAs which influence each other to shuffle around, and most of the related code and the structures themselves are generated via AST macros, you better be really comfortable with assembly analysis and debugger scripting. [1] - https://github.com/Hirrolot/datatype99 https://github.com/Hirrolot/datatype99
- kazinator 1y agoDebugging complex Lisp metaprogramming is a piece of cake in the overall debugging landscape. - It is not distributed across machines. No distributed fault tolerance issues. - It doesn't run on a resource constrained embedded system, with poor visibility, but in your dev environment. - It runs in a single thread; no flaky race conditions or deadlocks to debug. - Its expansion-time performance rarely matters; and it is rarely bogged down with machine dependencies or other low-level stuff. - It is unit-testable with cases does construct X expanding to construct Y. I would rather debug a macro than any number of other things I've also debugged.
- kristoff_it 1y agoDepends on the project, the general recommendation is to use metaprogramming like salt: just a pinch to add some flavor. Newcomers sometimes go bonkers with it though.
- lerno 1y agoIsn't all sorts of stuff metaprogramming in Zig? Generic types, vtable interfaces, printf. All use comptime to generate code.
- throwawaymaths 1y agoi think "using comptime" as a definition of metaprogramming is not really accurate... for example, i think calling this metaprogramming is really stretching it: var x: [sqrt(LENGTH)]f32 = undefined i think calling "instantiating a generic type" meta-programming is weak, since you're not doing any of the metaprogramming yourself, though i would concede that if debugging is the concern there is a point there. same goes for "using std.debug.print"
- Zambyte 1y agoI haven't read the article yet, but this seems to conflate compile time evaluation with metaprogramming. Not all comptime evaluation is metaprogramming. I would argue the majority of comptime programming as it is used in Zig isn't even metaprogramming. Metaprogramming is a paradigm where there source or target data structure is primarily the language you're programming in. comptime evaluation of expressions that could be runtime evaluated is not that. I would argue that comptime evaluation of expressions that return comptime only values (like types) is not even inherently that. Where you start to see actual metaprogramming in Zig is in functions like @typeInfo, @alignOf, @offsetOf, which actually let you operate on language constructs (ie: looping over the fields of a struct). This is not extremely out of the ordinary, but it's not done "as much as possible". Another place that metaprogramming shows up in Zig is code generation using the build system. I've never done this, and I would argue this is usually recommended to be done as little as possible, rather than as often as possible. Edit: I read the article, and while I think that the author was wrong about Zig pushing metaprogramming "as much as possible", they were fairly accurate in how it is used in the rest of the article, like for serialization. The thing is, most of my programs are not serialization. I use serialization as a means to do other things.
- throwawaymaths 1y ago@alignOf is almost never metaprogramming, it's almost always just something you have to call to declare to the compiler, yes (eyeroll), i am aware that this type erased pointer is aligned correctly. however, it's also a good signal "please review this code to make sure i didn't f it up"