Antonio has a post about generalizing environment classes (that capture state of a closure) with using generic types, such that the class itself captures only the arity of the environment.
To quote:
http://rotor.di.unipi.it/cisterni/Lists/My%20Blog/DispForm.aspx?ID=15
In general we may think that the compiler generates several environment classes, for the needed arities; for instance:
class Environment3<A, B, C> {
A a;
B b;
C c;
}
There is one issue that I can think of, off the top of my head – the CLR has a an excellent approach to generics (among the best I have seen) and is well described in Don Syme’s paper here:
The Design and Implementation of Generics for the .NET Common Language Runtime
http://research.microsoft.com/projects/clrgen/generics.pdf
the thing about CLR generics is that they are very efficient for all reference types, because for reference types there is no specialization of templating behavior (as with classical c++ style generics). All reference types use the class definition during runtime.
However value types cause the runtime to generate specialized classes to handle type of a value type that is used is a templated entity. Classes definitions are shared by value types only when they share the same foot print with respect to the GC.
So it would be better is compiler actually generated specialized classes to hold environment state whenever is knows that the types the environment needs to hold are value types. This simply provides for a performance benefit, because the specialization of the class will not happen at runtime, instead will be done at compile time.
Antonio also discusses a private member access issue – again I don’t think I fully get him. Assuming the new delegate mechanism is in place we could have classes that look like this
class Env
{
//have only public members
class Foo
//original method
void bar()
//anonymous compiler generated method
void anon_bar()
//access all members of Foo here
//access all public members of Env here
Is there a need to make members to Env private? The entire point of having Env is simply to act as a place holder for some values. Better yet (I don’t know if the old friend method mechanism works), but if friend decls are possible then the anonymous method can be declared as a friend in class Env. This does not add to the class definition in any way, it would simply allow for member access.
You might want to look at these links to follow the sequence of these posts –
1) Closures in CLR 2.0
2) Implementation of Closures (Anonymous Methods) in C# 2.0 (Part 6)
3) More on CLR 2.0 closures
4) Closure implementation enhancement in CLR 2.0 using the new delegate mechanism
5) Again on closures
6) this post
Remember Me
a@href@title, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Roshan James
E-mail