Ayende @ Rahien

Refunds available at head office

The difference between meta programming and IL weaving

Ivan also posted a response to Jeremy's C# vNext features, and he said something that caught my eye:

5. Metaprogramming => no objection here although you can do it now with reflection emit

No, there is a big difference between reflection emit and byte code weaving. Reflection emit is done after compilation is completed, meta programming occurs during compilation. This matters because it means that your code cannot refer to the results of the change being made.

A case in point, I am using IL Weaving to add the Dispose method to a class, but because I am doing it after compilation is completed, I cannot call the Dispose() method on the class, or put it in a using statement, in the same assembly, I would get a compiler error, because the method (and the interface) are not there yet.

Using meta programming, the compiler will know that those now exist, and it can not start using it, because it happened during the compilation process.

The implication of that are pretty significant, if you are talking about what you can and can't do in terms of enriching the language.

Comments

Scott Bellware
11/27/2007 03:06 AM by
Scott Bellware

I know this is probably such an obvious comment to make that it might just be completely lame, but...

All these rules are different in a dynamic language like IronRuby, where ensuring the presence of invocation sites is done at run time... or where you might be able to add the implementation of IDisposable at runtime by calling Dispose().

I've heard you say that all you need is an IDynamicObject interface and support for it in the runtime to achieve what Ruby can achieve.

Not knowing the depths of the details of what you have in mind, would something like IDynamicObject.MethodMissing() give you what you need? Does it mean that the compiler would switch to a duck typing mode when it encounters a class that implements IDynamicObject?

Ayende Rahien
11/27/2007 06:41 AM by
Ayende Rahien

IDynamicObject.MethodMissing would go a long way, but it solves only a part of the problem, actually.

Adding members, interfaces, etc, to the code is just as important.

That is not something that you can do with a method missing.

Ko
11/27/2007 08:04 AM by
Ko

And what about post-compilation weaving such as done by PostSharp? Granted, still not the first-class support achievable with meta-programming, but goes a long way to improve upon runtime weaving.

Scott Bellware
11/27/2007 08:27 AM by
Scott Bellware

That is not something that you can do with a method missing

If a member can be added to an object at runtime, it could be added from MethodMissing.

It would seem like C# would need duck typing regardless of where new members are added from at runtime.

Is duck typing out of bounds for what you envision for a meta programming solution?

Ayende Rahien
11/27/2007 02:41 PM by
Ayende Rahien

Ko,

No, post compile is not sufficient, that is basically IL Weaving, and it is not working, because you have no way to refer to the results of this method.

Ayende Rahien
11/27/2007 02:41 PM by
Ayende Rahien

Scott,

Given meta programming, I can add duck typing, the other way around is where I get into trouble.

Comments have been closed on this topic.