I have read in a few places that its possible to use the Expression tree in C# to optimize property getter code based on reflection. However, since the C# expression tree's are missing the ability to do assignment I was wondering if F# quotations could be used to do the same thing. However, I have not found any information on if the F# quotations can be compiled to IL like the C# Expression trees.

Just as an aside: at least in .NET 4 you can use Expression.Assign to express assignments. See also [link:bartdesmet.net]

F# quotations can be compiled to lambdas (or more precise: anonymous delegates), if you use the F# PowerPack (you need to reference FSharp.PowerPack.Linq.dll). However I don't know how far they support the new "statement-expressions" from .NET 4.0. Maybe a quotations guru can answer this question.

Update: I played a bit with with the quotations compile-thing and at least I found something that doesn't work:

1
2
3
4
5
6
let testExpr = <@ fun x ->
    let mutable r = 0
    r <- x
@>

Microsoft.FSharp.Linq.QuotationEvaluator.Compile(testExpr)

This throws:

System.NotSupportedException: Could not convert the following F# Quotation to a LINQ Expression Tree
--------
VarSet (r, x)
-------------

at Microsoft.FSharp.Linq.QuotationEvaluation.ConvExpr(ConvEnv env, FSharpExpr inp)
at Microsoft.FSharp.Linq.QuotationEvaluation.ConvExpr(ConvEnv env, FSharpExpr inp)
at Microsoft.FSharp.Linq.QuotationEvaluation.ConvExpr(ConvEnv env, FSharpExpr inp)
at Microsoft.FSharp.Linq.QuotationEvaluation.ConvExpr(ConvEnv env, FSharpExpr inp)
at Microsoft.FSharp.Linq.QuotationEvaluation.CompileImpl[a](a e, Boolean eraseEquality)
at <StartupCode$FSI_0009>.$FSI_0009.main@()
Stopped due to error

Setting a mutable variable doesn't seem to be supported (yet).

By on 5/30/2010 8:11 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper