F# 中的工作流构建器不使用接口是否有原因?

发布于 2024-11-03 02:43:25 字数 76 浏览 3 评论 0原文

这是出于好奇而提出的问题:当您实现工作流工厂时,您不会将其作为接口实现来执行,而只是确保 monad 函数的函数签名匹配。这有设计原因吗?

This is a question just out of curiosity: when you implement a workflow factory, you don't do it as an interface implementation, but rather just make sure the function signatures of the monad functions match. Is there a design reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

墨落成白 2024-11-10 02:43:25

一方面,.NET 中缺乏更高级的类型意味着您无法为这些方法提供有用的签名。例如,ListBuilder.Return 应该具有类型 't -> 't list,而 OptionBuilder.Return 的类型应为 't ->; 't选项。无法使用 Return 方法创建具有支持这两种方法的签名的接口。

For one thing, the lack of higher-kinded types in .NET means that you can't give the methods useful signatures. For instance, ListBuilder.Return should have type 't -> 't list, while OptionBuilder.Return should have type 't -> 't option. There's no way to create an interface with a Return method that has a signature supporting both of these methods.

数理化全能战士 2024-11-10 02:43:25

我认为 kvb 提到的缺乏更高级的类型可能是主要原因。有多种方法可以解决这个问题,但这会使代码有点晦涩(请参阅此代码段)。

另一个原因是 F# 计算表达式允许您定义不同的方法组合。它并不总是只是BindReturn。例如:

  • 有些定义 YieldYieldFromCombine 以允许生成结果
  • 有些定义 ReturnReturnFrom, Bind 定义一个 monad
  • 有些定义 Return, ReturnFrom, Bind, Combine 定义一个可以返回多个东西的 monad
  • 有些还定义 DelayDelayRun 来处理懒惰

......所以计算表达式需要定义为很多不同的接口。我认为当前的设计在您可以支持的计算功能方面留下了一些很好的灵活性。

I think that the lack of higher-kinded types as mentioned by kvb is probably the main reason. There are ways to workaround that, but that makes the code a bit obscure (see this snippet).

Another reason is that F# computation expressions allow you to define different combinations of methods. It's not always just Bind and Return. For example:

  • Some define Yield, YieldFrom, Combine to allow generating results
  • Some define Return, ReturnFrom, Bind to define a monad
  • Some define Return, ReturnFrom, Bind, Combine to define a monad that can return multiple things
  • Some also define Delay or Delay and Run to handle laziness

... so computation expressions would need to be defined as quite a few different interfaces. I think the current design leaves some nice flexibility in what features of computations you can support.

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