Expression<Func<A,R>> to Func<A,R> conversion

Since the compiler is able to generate both


Expression<Func<int, string>> e = i => i.ToString()

 


and


Func<int, string> f = i => i.ToString()

 


I think it would be very helpful to have a way to get the Func<,> delegate for a given Expression<Func<,>>

Easiest way would be by an implicit conversion

f = e;
 


I understand it is the job of the compiler to generate either the Expression or the Func and I saw the Espresso sample in the LINQ preview where a pretty elaborated parser has been created in order to do so.

As the sample shows, there is obviously the need for that kind of conversion and look forward to see a more standard approach to do so.


Answer this question

Expression<Func<A,R>> to Func<A,R> conversion

  • StephenMas

    Check out the Espresso sample.  It has code written to do just that. It's runtime intepretted though, and needs a bit more work.

  • RomanG

    I think a conversion method on Expression itself would be more useful than a Linq-linked (rimshot) extension method.  Extension methods are little more than compiler magic, which restricts the aesthetics of these methods to those that know that Foo.Bar(baz) can be rewritten as Gleep.Bar(foo, baz).

    Were expression trees something of limited usefulness outside of Linq, I think I wouldn't care.  However, such trees could see use in computer algebra systems, scripting languages, etc.  Particularly if the CodeDOM parsers can start outputting expression trees, or if the trees can be convertable from CodeDOM trees (which I think you'll have to involve at some point in this conversion effort, anyway).  These may easily involve languages that don't know about extension methods.

  • windsamurai

    As I stated before, I saw it.

    I hoped it would be part of the LINQ project itself, either compiler generated, provided by an extension method or conversion operator on Expression, ...


  • Vassilux

    We have not determined yet if that will be part of the product or not. Feedback like yours will be influential.

  • Expression<Func<A,R>> to Func<A,R> conversion