代码引用和表达式树

发布于 2024-12-15 22:09:06 字数 69 浏览 1 评论 0原文

我想知道这两个功能在底层的实现方式是否有什么区别?即,不只是建立在旧的良好表达式树之上的代码引用吗?

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-12-22 22:09:06

这两种类型非常相似,但表示方式不同。

  • 报价以更实用的方式设计。例如 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 applications App(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 for let rec declarations

  • Quotations 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文