[Mono-dev] gmcs and The Future
Scott Peterson
scott at ssblack.co.nz
Wed Feb 4 04:56:40 EST 2009
Generic type variance has landed in gmcs SVN [1]. This is a C# 4
feature. It can be enabled by passing -langversion:future. While type
variance is cool, I want to talk about -langversion:future. I see this
as being a sentinel langversion for features as yet unreleased by
Microsoft. Those features could be announced additions to the language
(as with generic type variance, or the "dynamic" keyword) but they
need not be. Mcs has done an excellent job of tracking the official C#
language, and it will continue to do so, but the Mono project has a
world-class compiler entirely at its disposal. We need not confine
ourselves to the blessed specs of Microsoft or Ecma. We could "trick
out" C#, indulging in sugars of our own device, so long as we keep our
creations in -langversion:future. Those projects which are willing to
sacrifice compatibility with csc in order to partake of our forbidden
fruit can write code in this New C#. This C#++. This
-langversion:future.
To be clear, I am not advocating VM changes. Anything to come out of
this compiler would remain binary-compatible with the .NET runtime.
Language features and APIs only. I have loads of ideas - I actually
keep a list where I jot down every language feature idea I come up
with - but I want to hear from the list. Do you like the idea? Do you
have concerns? Do you have a language feature you've always wanted?
I will start the ball rolling with a simple feature:
Problem: While the 'typeof' keyword is very convenient for getting
Type objects, it is much more difficult to get any other kind of
reflection data. For example, to get a MethodInfo object, one usually
does:
> static readonly MethodInfo methodInfo = typeof(Foo).GetMethod("Bar", new Type [] { typeof(string), typeof(int) });
Not only is this an eyesore, but we have a method name in a string. If
I refactor that method, I will have to remember to update this code as
well.
Solution: The "reflect" keyword. This is like the typeof keyword, but
you can use it to reflect upon anything.
Examples:
> Type fooType = reflect(Foo);
> MethodInfo barMethod1 = reflect(Foo.Bar(string));
> MethodInfo barMethod2 = reflect(Foo.Bar(string,int));
> FieldInfo batField = reflect(Foo.bat);
> PropertyInfo bazProperty = reflect(Foo.Baz);
> ConstructorInfo fooCtor1 = reflect(new Foo());
> ConstructorInfo fooCtor2 = reflect(new Foo(string));
> ConstructorInfo staticFooCtor = reflect(static Foo());
> EventInfo fooEvent = reflect(Foo.Changed);
And if I refactor any of these members or types, my IDE will update
this code as well.
Your turn.
- scottp
[1] http://themonkeysgrinder.blogspot.com/2009/02/c-4-is-now.html
More information about the Mono-devel-list
mailing list