Let’s say we have a simple F# quotation:
type Pet = { Name : string }
let exprNonGeneric = <@@ System.Func(fun (x : Pet) -> x.Name) @@>
The resulting quotation is like:
val exprNonGeneri : Expr =
NewDelegate (System.Func`2[[FSI_0152+Pet, FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
x, PropertyGet (Some (x), System.String Name, []))
Now I want to generalize it, so I instead of type “Pet” and property “Name” I could use an arbitrary type and method/property defined on it. Here is what I am trying to do:
let exprGeneric<'T, 'R> f = <@@ System.Func<'T, 'R>( %f ) @@> let exprSpecialized = exprGeneric<Pet, string> <@ (fun (x : Pet) -> x.Name) @>
The resulting expression is now different:
val exprSpecialized : Expr =
NewDelegate (System.Func`2[[FSI_0152+Pet, FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
delegateArg,
Application (Lambda (x,
PropertyGet (Some (x), System.String Name, [])),
delegateArg))
As you can see, the difference between the first and the second expression is that in first case the top level NewDelegate expression contains PropertyGet, while the second expression wraps PropertyGet in a Application/Lambda expression. And when I pass this expression to an external code it does not expect such expression structure and fails.
So I need some way to build a generalized version of quotation, so when it gets specialized, the resulting quotation is an exact match of <@@ System.Func(fun (x : Pet) -> x.Name) @@>. Is this possible? Or it there only choice to manually apply pattern matching to a generated quotation and transform it to what I need?
UPDATE. As a workaround I implemented the following adapter:
let convertExpr (expr : Expr) =
match expr with
| NewDelegate(t, darg, appl) ->
match (darg, appl) with
| (delegateArg, appl) ->
match appl with
| Application(l, ldarg) ->
match (l, ldarg) with
| (Lambda(x, f), delegateArg) ->
Expr.NewDelegate(t, [x], f)
| _ -> expr
| _ -> expr
| _ -> expr
It does the job – I can now convert expression from 1st to 2nd form. But I am interested in finding out if this can be achieved in a simple way, without traversing expression trees.
I don’t think it will be possible to do this; in the second case, you are plugging in the expression
<@ (fun (x : Pet) -> x.Name) @>, which is represented using aLambdanode, into the hole in the other expression. The compiler does not simplify expressions during this plugging process, so theLambdanode won’t be removed no matter what you do.However your pattern matching workaround can be greatly simplified:
In fact, your more complicated version is incorrect. This is because the
delegateArgin your innermost pattern is not matching against the value of the previously bounddelegateArgidentifier from the outer pattern; it is a new, freshly bound identifier which also happens to be calleddelegateArg. In fact, the outerdelegateArgidentifier has typeVar listwhile the inner one has typeExpr! However, given the limited range of expression forms generated by the compiler your broken version may not be problematic in practice.EDIT
Regarding your followup questions, if I understand you correctly it may not be possible to achieve what you want. Unlike C#, where
x => x + 1could be interpreted as having a type of eitherFunc<int,int>orExpression<Func<int,int>>, in F#fun x -> x + 1is always of typeint->int. If you want to get a value of typeExpr<int->int>then you generally need to use the quotation operator(<@ @>).There is one alternative that may be of use, however. You can use the
[<ReflectedDefinition>]attribute on let bound functions to make their quotations available as well. Here’s an example: