这是在 C# 中可以做的事情 -
Here's what can be done in C# -
var two = 2; System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two; expr.Compile().Invoke(4); // returns 8我希望在 F# 中做精确的等效.这是我尝试过的,但没有编译 -
I wish to do the precise equivalent in F#. Here's what I tried, but did not compile -
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也许可以预见,编译在第 2 行失败并出现以下错误 -
Perhaps predictably, compilation fails on line 2 with the following error -
"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>> ^^^^^^^^^^^^^^^^ 推荐答案我不知道您为什么要避免使用 F# 引用 - 在掩护下,它们与 C# 表达式树几乎相同,如果您愿意要在 F# 中创建表达式树,编译器将在任何情况下使用引号...
I'm not sure why you want to avoid using F# quotations - under the cover, they are pretty much the same thing as C# expression trees and if you want to create an expression tree in F#, the compiler will be using quotations under the cover in any case...
无论如何,您无需编写显式的 <@ .. @> 即可执行此操作,因为编译器可以在将函数作为参数传递给方法时自动引用该函数.所以你可以这样做:
Anyway, you can do this without writing explicit <@ .. @> because the compiler can automatically quote a function when it is passed as an argument to a method. So you can do:
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但是,这确实编译为包含在调用中的 F# 引用,该调用将其转换为 C# 表达式树.所以,最后,你会得到和你写的一样的东西:
However, this really compiles to an F# quotation wrapped in a call that converts it to C# expression tree. So, in the end, you'll get the same thing as if you wrote:
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更多推荐
如何使用 F# lambda 创建 Linq 表达式树?
发布评论