What is the difference between metaprogramming and reflection?

Asked

Viewed 379 times

3

There are two terms that look a lot like when we are talking about introspection techniques: metaprogramming and reflection.

Wikipedia treats reflection as a key strategy of metaprogramming (source), but for me these terms are identical and I don’t know what differs them.

What is the difference between the terms? There is reflection without metaprogramming or vice versa?

1 answer

4


I disagree with the definition of Wikipedia. The metaprogramming text seems to be a little better.

Reflection is a mechanism which gives information about the code itself and allows it to be modified. This can occur at runtime or compile time, what matters is access to the information to manipulate it (if not manipulate it is only introspection). It is only more common to occur at runtime.

Metaprogramming is a paradigm which allows you to manipulate the code more generally, you program how the code should be programmed. Reflection is often used to get the necessary information and change what you need, but it’s not the only way. And it is more common to occur at compile time.

There are other mechanisms that provide the same ability of the code to be aware about itself. One of them is the use of a meta language or extensible language. But it might just be Generics or templates.

The paradigm is putting out wings and languages mainstream They’re getting closer and closer to him. But beware, it’s too hard to use it right, so they’ve always avoided its use, even though it’s been around for over 60 years.

Of course, in each context the definition can vary a little. In more academic environment will be more widespread, in some communities will pull the sardine to the side of how it uses. Some disagree that what languages mainstream. as Java and C#. have actual reflection, at most have a library that helps in a part of the code, ie JEE or . NET that is reflective.

I that m uses the term metaprogramming more when reflection is carried out at compile time, but it does not seem to be universally accepted.

  • 1

    I was about to look for the difference between introspection and reflection, but your first few paragraphs killed the doubt

  • There is reflection without metaprogramming?

  • @vnbrs good question :) Maybe that’s what library-based thinking is, but I’m not able to say.

  • @Maniero found a question exactly about this in the Soen https://stackoverflow.com/questions/7641881/is-metaprogramming-a-subset-of-reflection

  • 1

    @vnbrs then... there are controversies. And I think he’s wrong, at least in C# is able to do all the reflection through the library. I found the answer somewhat confusing and somewhat contradicts the first sentence. I don’t think it’s easy to say precisely because I’ve never seen a canonical and authoritative response.

  • In C# you can create on the fly executable code, using the System.Reflection.Emit namespace classes, as well as using the System.Expressionsexpressions. So in C# running on . NET you can use reflection, but also create code new procedures that has not been statically programmed.

  • @Albertomonteiro this is outdated, today has much better thing.

  • Like what @Maniero ?

  • @Albertomonteiro Roslyn.

  • Roslyn is pretty cool, but it doesn’t take away the utility of the other methods I mentioned, for example, EF Core uses and abuses Expressions to be able to convert C# code into SQL queries, they don’t use Roslyn for this, as well as Fody, which allows making IL Weaving also doesn’t use Roslyn. I believe that everyone has their own space. But in most cases I don’t think of Roslyn as the first option for metaprogramming. And none of the methods I’ve talked about are outdated.

  • Misfortune of such technologies.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.