Expression
I looked previously at Expression on the CTP; well, guess what? They went and renamed everything... here's the previous example, updated; note the use of Expression.Block and Expression.Assign (rather than Comma and AssignProperty):
class Foo
{
public string Bar { get; set; }
public override string ToString()
{
return Bar;
}
}
static void Main()
{
var param = Expression.Parameter(typeof(Foo), "foo");
var assign = Expression.Assign(
Expression.Property(param, "Bar"),
Expression.Constant("def", typeof(string)));
var writeLine = Expression.Call(
typeof(Console).GetMethod("WriteLine", new[] { typeof(object) }),
param);
var action = Expression.Lambda<Action<Foo>>(Expression.Block(assign, writeLine), param);
var actionDel = action.Compile();
Foo foo = new Foo();
actionDel(foo);
}
It is also pretty clear that there are no plans to add language support for this (via a lambda); maybe something for the future.
Dynamic
I think "dynamic" is great if you are talking to COM or DLR; but should generally be avoided otherwise. Still; one interesting aspect is that it provides access to generic operators; which wasn't yet implemented on the last CTP. Well; it is here now - and it actually doesn't perform too badly... to avoid taking a dependency on an extra dll, I'd consider it...
int
Operator: 833ms (100000010)
Dynamic: 1383ms (100000010)
float
Operator: 786ms (3.355443E+07)
Dynamic: 1309ms (3.355443E+07)
decimal
Operator: 3974ms (100000010)
Dynamic: 3911ms (100000010)
As it happens the Nullable<T>performance isn't quite as good yet, but you can always use MiscUtil's Operator class ;-p
Other than that; unless I start writing lots of IronPython, I currently don't expect to use "dynamic" all that much.
4 comments:
Marc
There is another scenario where dynamic is really useful and that is interacting with other dynamic languages even without the DLR. Examples of this are when dealing with javascript and in particular javascript interaction in silverlight.
True, true; I remember Anders' demo.
Interestingly enough, the performance aspect is part of the reason why the DLR exists. After all, there's not really any reason why you couldn't just store a bunch of objects and constantly cast them to what they really are. I believe the DLR essentially does this, but does some caching to avoid having to cast so much.
Well, *true* DLR can do a lot more than that - adding members on the fly, etc... but it is a factor.
Post a Comment