Live data from Hacker News

Macros on Steroids: How pure C can benefit from metaprogramming

hirrolot.github.io

41–50 of 63 posts

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#41
post #6

Earlier quoted context omitted.

Only if you can switch to something like Zig, Rust, etc. But a tremendous amount of code bases are already written in plain C. Using preprocessor metaprogramming allows you to invoke macros right in the same files in which you write ordinary C code, without the need for integration with other languages.

To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…

When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#42

Earlier quoted context omitted.

To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…

When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.

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 only a subset, I don't know C as well as I'd like, so it would be interesting to see something that the C preprocessor handles better.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#43
post #11

Earlier quoted context omitted.

I humbly agree unless, of course, there is no other solution, e.g. lack of compilers on your platform.

Even so, I used to do a lot of metaprogramming with the C preprocessor back in the day. Over time, I grew disillusioned with it and gradually removed it from my C code. It just wasn't worth it. P.S. I also did metaprogramming in C by writing C programs to generate C code, such as converting an array of structs to multiple arrays, one for each field (for efficiency). This worked out fairly cleanly, but with D's abilit…

I'm curious of your opinion about AdaCore CCG, an SPARK to C compiler, for targets that don't have an Ada compiler yet.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#44
post #20

You could create this as a frontend macro preprocessor that implements new language constructs. Maybe it could be called cfront. I just hope that the usefulness of that doesn't lead to bloated and complex codebases and binaries as features are added to the preprocessor.

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

#45

Earlier quoted context omitted.

Even so, I used to do a lot of metaprogramming with the C preprocessor back in the day. Over time, I grew disillusioned with it and gradually removed it from my C code. It just wasn't worth it. P.S. I also did metaprogramming in C by writing C programs to generate C code, such as converting an array of structs to multiple arrays, one for each field (for efficiency). This worked out fairly cleanly, but with D's abilit…

I'm curious of your opinion about AdaCore CCG, an SPARK to C compiler, for targets that don't have an Ada compiler yet.

I do not know enough about them to have a worthwhile opinion.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#46

Earlier quoted context omitted.

To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…

When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.

I'm a huge fan of java because of reflection. It can be so empowering and awesome. But lately I've come to prefer codegen+JIT when possible. So, discovery through introspection, and to call/read/modify stuff I'll generate the code.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#47
I have never had a problem generating C code using C itself. (A fancy way to do this would be to have an ASP-like preprocessor, i.e. a simple program that processes code blocks; this is essentially "inverse quoting," whereby everything that is outside the brackets becomes the contents of a string literal.)

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#48

Earlier quoted context omitted.

I'm curious of your opinion about AdaCore CCG, an SPARK to C compiler, for targets that don't have an Ada compiler yet.

I do not know enough about them to have a worthwhile opinion.

OK sorry :-) thanks

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#49
One 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 this tool to make our tests more awesome.

Re: Macros on Steroids: How pure C can benefit from metaprogramming

#50

Earlier quoted context omitted.

When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.

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 also do tricks with reflection but that's way more complex to implement.
Post reply on HN