Macros on Steroids: How pure C can benefit from metaprogramming
51–60 of 63 posts
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#52Something about macros that I don't see discussed much is the trade-off between improved clarity of intent vs learning curve for contributors. I don't have a ton of experience w/ C, but the large projects I've seen appear to have a tendency to avoid heavy macro usage (e.g. using `void *` for hashmap implementations instead of reaching for macros, for example). I see macros appear more frequently among those that prio…
For example, I think x-macros can be super useful and a consistent way to define certain data structures.
But using macros to define your own special DSL and make C code not look like C code is pretty nasty.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#53Earlier quoted context omitted.
There are several reasons why not to resort to third-party code generators [1]. This includes the burden of maintenance (think about the build process) and seamless integration of native macros with other code. Do you have some code to show bloated binaries with Datatype99/Interface99? The last time I checked the generated assembly code, it was nearly of the same size as hand-written code [2]. [1]: https://github.com…
Original comment was satirizing C++, it started as a (relatively) simple code generator that consumed a custom OOP variant of C and produced pure C. It grew into... well C++.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#54My not-so-humble opinion is that if you're using the C preprocessor to do metaprogramming, you have outgrown C and need a more powerful language.
I agree that you shouldn't use the preprocessor for metaprogramming, it sucks. Switching to C++ is not a solution either since its template system is garbage for metaprogramming aswell. Starting small with a small utility such as this one or creating one of your own is the best aproach in my opinion.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#55Earlier quoted context omitted.
If you need these kinds of advanced metaprogramming capabilities then I feel like that's a sign that maybe plain C isn't suitable for your project and you'd better off switching to Zig, Rust, D or C++. These all provide powerful compile-time execution capabilities and the ability to expose C interfaces.
Even those may not be powerful enough. I have a use case where I was trying to read specification files to generate unicode tables. But using something like zig's @embedFile to get a handle to a []u8 in a comptime block doesn't work because I don't want the file to be embedded into the binary; I want something more akin to turing complete code generation. And I don't need it to run it on every compile, as the entire…
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#56One use case that I'm really excited about considering this for is testing! I'm currently working on a project that uses a very straightforward C style and for the mainline processing we don't see a lot of duplication problems and aren't using macros much at all. However, as we try hard to get high unit test coverage I often find myself doing "macro magic" in my tests to remove boilerplate, and I'm excited to try out…
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#57Something about macros that I don't see discussed much is the trade-off between improved clarity of intent vs learning curve for contributors. I don't have a ton of experience w/ C, but the large projects I've seen appear to have a tendency to avoid heavy macro usage (e.g. using `void *` for hashmap implementations instead of reaching for macros, for example). I see macros appear more frequently among those that prio…
My issue with macros is that how they are used can vary greatly because it is so flexible. I’ve worked in a codebase where we treated macros like someone might treat C++ templates and it was great. For example, I think x-macros can be super useful and a consistent way to define certain data structures. But using macros to define your own special DSL and make C code not look like C code is pretty nasty.
[1]: https://hirrolot.github.io/posts/extend-your-language-dont-a...
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#58Something about macros that I don't see discussed much is the trade-off between improved clarity of intent vs learning curve for contributors. I don't have a ton of experience w/ C, but the large projects I've seen appear to have a tendency to avoid heavy macro usage (e.g. using `void *` for hashmap implementations instead of reaching for macros, for example). I see macros appear more frequently among those that prio…
> vs learning curve for contributors Early on in my programming career, I implemented all sorts of beautiful magic, but I was alone. Later, I realized that beautiful magic was keeping me alone. Now, much later in my career, I program mostly like a novice. I don't write clever code, I unroll powerful one liners into boring for loops, and I use all sorts of temporary variables to make intent perfectly clear, because I…
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#59Earlier quoted context omitted.
Could I beg of you to give an example? Back when I was working in C#, there were quite a few times when I'd encounter things being done with reflection that I wish had been done with less code and a more type safe manner by just... not using reflection. I always just assumed a lot of that was just bad habits left over from the .NET 1.0 days, back before generics. So I'm curious if there's much overlap there? If it's…
For example INotifyPropertyChanged which is used in WPF. The typical implementation is something like this: public string CustomerName { get { return this.customerNameValue; } set { if (value != this.customerNameValue) { this.customerNameValue = value; NotifyPropertyChanged(); } } } When you have 20 properties you have a ton of repetitive code. With a preprocessor I could reduce each property to a one liner. You can…
Or were you really just meaning to advocate for metaprogramming in general?
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#60Can anyone with some real experience with this comment on the debug visibility? In gdb or lldb or otherwise? How easy is it to see what is going on, partially expand macros, get at intermediate values, or evaluate things at the debug prompt? At first glance it seems like it'd be quite obfuscated but perhaps I will be pleasantly surprised by some experience reports.
If you're asking about debugging macros, I've put a huge effort to this [1]. There are several macros that help you test, debug, and report errors at compile-time. For example, you can make calls to ML99_abort and see the expansion with -E. Usually I debug macros in this way. If you're asking about debugging code generated by macros, Interface99 has no problems with it since the generated code is trivial, pretty much…