Define a new Class

Jan 9, 2009 at 11:21 AM

How do I define a new data type in the DLR? I would like to know how to define a .NET class with member variables and a way to check if an object is of this type.

For example, how do you compile this code in an imaginary language to the DLR:

class Person { name, email, age }

something = new Person("joe", "joe@xyz.com", 34)

if(something isa Person) ...

Thanks for your help,

Jules

Coordinator
Jan 9, 2009 at 6:27 PM

At a high-level, you would generate .NET class to represent your new data type.  You would implement IDynamicObject on that class and provide a DynamicMetaObject to handle operations on your objects.  You could put other internal interfaces (such as Pythons IPythonObject, hand wave on interface name) to handle other operations or ‘istype’ operators you’d like to add to your language.  For a serious detailed look at a tuned implementation, you could look at the IronPython sources for how they parse and produce the Expression Trees for a python class definition.

Bill

From: JulesJacobs [mailto:notifications@codeplex.com]
Sent: Friday, January 09, 2009 10:51 AM
To: Bill Chiles
Subject: Define a new Class [dlr:43810]

From: JulesJacobs

How do I define a new data type in the DLR? I would like to know how to define a .NET class with member variables and a way to check if an object is of this type.

For example, how do you compile this code in an imaginary language to the DLR:

class Person { name, email, age }

something = new Person("joe", "joe@xyz.com", 34)

if(something isa Person) ...

Thanks for your help,

Jules

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

Jan 9, 2009 at 6:38 PM
There's no direct support in the DLR for defining a CLR type; you'll need to use Reflection.Emit.  You should be able to hook up a DLR expression tree to an emitted method by creating a LambdaExpression and then calling CompileToMethod to attach the expression tree to the MethodBuilder.
Jan 9, 2009 at 7:39 PM

Thanks for your answer. So I should use ModuleBuilder.DefineType to create a new type?

I do not need the dynamic object features in the DLR (the language I'm creating is dynamically typed but method dispatch doesn't work like in Ruby/Python). I'm looking at using the DLR because the expression trees are very convenient. Once a data type is defined it never changes (you cannot add additional attributes at run time). Is it possible to get the same speed as native .NET types (for example types defined in C#) for attribute access with IDynamicObject?

IronPython compiles a class definition to a method call that creates a new class at run time:

  MSAst.Expression classDef = Ast.Call(
AstGenerator.GetHelperMethod("MakeClass"),
AstUtils.CodeContext(),
Ast.Constant(SymbolTable.IdToString(_name)),
bases,
Ast.Constant(FindSelfNames()),
lambda
);

I'm not sure if this is the right way to go for me, because I'd like to execute class definitions at run time in an interactive environment (like the python shell or irb) but also compile code statically.

Is it better to use Reflection.Emit for all code generation in my case?

Thanks,

Jules

Jan 9, 2009 at 7:43 PM

There's no direct support in the DLR for defining a CLR type; you'll need to use Reflection.Emit. You should be able to hook up a DLR expression tree to an emitted method by creating a LambdaExpression and then calling CompileToMethod to attach the expression tree to the MethodBuilder.

Thank you. That answers my question :)

Jan 9, 2009 at 8:28 PM

There's no direct support in the DLR for defining a CLR type; you'll need to use Reflection.Emit. You should be able to hook up a DLR expression tree to an emitted method by creating a LambdaExpression and then calling CompileToMethod to attach the expression tree to the MethodBuilder.

It appears that Lambda.Expression.CompileToMethod only creates static methods.(1)  Is there any way to get non-static?


(1) Lambda Expression.CompileToMethod calls LambdaCompiler.CompileLambda(LambdaExpression,MethodBuilder,bool)  calls LambdaCompiler..CreateStaticCompiler(...) with false for parameter dynamicMethod, which constructs a LambdaCompiler, the ctor of which says:  

ContractUtils

 

.Requires(dynamicMethod || method.IsStatic, "dynamicMethod");

 

Jan 9, 2009 at 9:37 PM
Thanks dmiller. I opened a bug for this: http://www.codeplex.com/dlr/WorkItem/View.aspx?WorkItemId=1378.