Generating Class Types..

Feb 13, 2009 at 5:30 AM
Hello All- with one hand on SICP and another on my shaky c# experience(my background is primarily in java and lisp) I've been playing around with the DLR.

As a whole I think I understand the expression trees and the binders..but I'm having some trouble understanding how to most effectively(and simply) handle extending clr types. Outside of looking at IronPython and IronRuby(both of which seem much to large to serve as a simple example), does anyone on this list have any suggestions or simple examples? Something that could bolt onto Toyscript or something?

thanks
Mike Kohout
Coordinator
Feb 13, 2009 at 5:22 PM

I think the answer depends on what you mean.  If you mean deriving from a clr type, ipy does have scheme for consing and sharing derived clr type objects for getting better interop effects and indirecting to the py dynamic types.  I don’t recall the details, but if this is what you want, Dino could point you to the right code in ipy.  If you mean simply adding members to clr type objects or instances of clr types, then ipy won’t help you since they leveraged precedent in python to forbid extending clr types with dynamically added members.  I think Ruby supports this, and if this is what you want, Tomas can probably point you at his tricks for doing that.  There are the obvious wkptr tables mapping instances and types to extended members, but I’m not sure if there’s something more clever or support in our codeplex internal scope objects for these sorts of language-specific extensions to types and instances.

Bill

From: mwkohout [mailto:notifications@codeplex.com]
Sent: Thursday, February 12, 2009 9:31 PM
To: Bill Chiles
Subject: Generating Class Types.. [dlr:47024]

From: mwkohout

Hello All- with one hand on SICP and another on my shaky c# experience(my background is primarily in java and lisp) I've been playing around with the DLR.

As a whole I think I understand the expression trees and the binders..but I'm having some trouble understanding how to most effectively(and simply) handle extending clr types. Outside of looking at IronPython and IronRuby(both of which seem much to large to serve as a simple example), does anyone on this list have any suggestions or simple examples? Something that could bolt onto Toyscript or something?

thanks
Mike Kohout

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Feb 13, 2009 at 5:34 PM
The IronPython code that generates a new CLR type can be found mostly in the file NewTypeMaker.cs.  IronPython shares these types, so that two IronPython types which derive from System.Object will have the same underlying CLR type.  There's similar code in IronRuby.

When it comes to generating CLR types, the DLR doesn't (yet!) give you any assistance; you're pretty much back in the Reflection.Emit world.
Feb 16, 2009 at 8:26 PM

Curt and Bill are absolutely right. Just to expand on the answer a bit…

There are basically two things you have to think about for extending CLR types. The first is if your users want to inherit from interfaces and non-sealed CLR types, and pass instances of these types to other CLR methods. For that to work, you have to use Reflection.Emit to generate a derived type which inherits from the base type, and override/implement all of the virtual/abstract methods. The implementations should have calls back into your runtime to allow you to then call the appropriate function in the user’s derived class. That way, they can pass these instances to functions expecting the CLR type, and it will just work. The best example for this is IronPython’s NewTypeMaker.

The second case is Ruby-style open classes where you can extend the CLR type at runtime by adding methods/fields. Since CLR types are closed, you can’t actually add things to them, but you can instead track the additional methods in your own type system (which you probably have anyway). Then your runtime binder looks at this “extension method table” to bind calls. Conceptually it’s just a mapping from Type -> List of methods, but with version tracking, etc, it can get a bit complicated. The best example for this is IronRuby’s RubyModule/RubyClass and friends.

- John