代码引用和表达式树
我想知道这两个功能在底层的实现方式是否有什么区别?即,不只是建立在旧的良好表达式树之上的代码引用吗?
谢谢。
I wonder if there is any difference in how the two features are implemented under the hood? I.e. Aren't just code quotations built on top of the old good expression trees?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两种类型非常相似,但表示方式不同。
报价以更实用的方式设计。例如
foo a b
将表示为一系列应用程序App(App(foo, a), b)
引用可以表示一些仅在F# 和使用表达式树会隐藏它们。例如,有
Expr.LetRecursive
用于let rec
声明引用首次在 .NET 3.0 中引入。当时表达式树只能表示 C# 表达式,因此不可能轻松捕获所有 F# 构造(引用可以捕获任何 F# 表达式,包括命令式表达式)。
引用也被设计为可以使用递归轻松处理。
ExprShape
模块包含的模式允许您仅用 4 个案例处理所有可能的引用(这比在 C# 中使用数十个方法实现访问者模式容易得多)。当您有 F# 引用时,可以使用 FSharp.Quotations.Evaluator。如果您使用某些需要来自 F# 的表达式树的 .NET API,这非常有用。据我所知,没有相反的翻译。
The two types are quite similar, but they are represented differently.
Quotations are designed in a more functional way. For example
foo a b
would be represented as a series of applicationsApp(App(foo, a), b)
Quotations can represent some constructs that are available only in F# and using expression trees would hide them. For example there is
Expr.LetRecursive
forlet rec
declarationsQuotations were first introduced in .NET 3.0. Back then expression trees could only represent C# expressions, so it wasn't possible to easily capture all F# constructs (quotations can capture any F# expression including imperative ones).
Quotations are also designed to be easily processible using recursion. The
ExprShape
module contains patterns that allow you to handle all possible quotations with just 4 cases (which is a lot easier than implementing visitor pattern with tens of methods in C#).When you have an F# quotation, you can translate it to C# expression tree using FSharp.Quotations.Evaluator. This is quite useful if you're using some .NET API that expects expression trees from F#. As far as I know, there is no translation the other way round.