如何使用F#lambda创建Linq表达式树?

Bry*_*dds 16 c# linq f# expression-trees

这是C#可以做的 -

var two = 2;
System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two;
expr.Compile().Invoke(4); // returns 8
Run Code Online (Sandbox Code Playgroud)

我希望在F#中做到精确的等价物.这是我尝试过的,但没有编译 -

let two = 2
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
expr.Compile().Invoke(4) // desired to return 8
Run Code Online (Sandbox Code Playgroud)

也许可以预见,编译在第2行失败,出现以下错误 -

"This function takes too many arguments, or is used in a context where a function is not expected."
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
            ^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 21

我不确定为什么要避免使用F#引用 - 在封面下,它们与C#表达式树几乎相同,如果要在F#中创建表达式树,编译器将在封面下使用引号在任何情况下...

无论如何,您可以在不明确编写的情况下执行此操作,<@ .. @>因为编译器可以在将函数作为参数传递给方法时自动引用函数.所以你可以这样做:

type Expr = 
  static member Quote(e:Expression<System.Func<int, int>>) = e

let two = 2
let expr = Expr.Quote(fun x -> x * two) 
expr.Compile().Invoke(4) // desired to return 8
Run Code Online (Sandbox Code Playgroud)

编辑:但是,这实际上编译为包含在调用中的F#引用,该调用将其转换为C#表达式树.所以,最后,你会得到和你写的一样:

open Microsoft.FSharp.Linq.RuntimeHelpers

let two = 2
let expr = 
  <@ System.Func<_, _>(fun x -> x * two) @>
  |> LeafExpressionConverter.QuotationToExpression 
  |> unbox<Expression<Func<int, int>>>
expr.Compile().Invoke(4) // desired to return 8
Run Code Online (Sandbox Code Playgroud)

  • 1)不能直接调用F#引用 - 因此您必须将其转换为C#表达式树并调用它.2)它比普通的F#慢,因为转换为表达式树会增加一些开销.我不知道到底有多少 - 没有做过任何测量.3)不确定 - 取决于C#表达式eval是否可用. (2认同)