Let us corrupt C# for code generators: The horror story of partial methods

time to read 5 min | 859 words

I think you can pretty much understand how I feel about this feature.

Think about it, a compile time only method declaration, that you can "override" in another method, like this:

partial class C

{

  static partial void M(int i); // defining declaration

  static partial void M(int i)  // implementing declaration

  {

  }

}

The crazy part is that the code below is not compiled unless you supply an implementation to the method, and the implementation is not neccesary.

C.M( something );

The reasoning behind that is to have light-weight events, that exists only when you "subscribe" to them. The reason for that is the Linq code generation features, which produce a lot of events.

So, this is basically compiler macros again, for a dubious reason at best. I don't think that the language should change because of a tool that it using it. If they want to make any useful changes, there are more than enough suggestions. Method Missing would be welcomed, as well as attributes that can accept useful parameters (i.e, not just String, Int32, Type).

Yuck!