F# 模块可以进行猴子修补吗?

发布于 2024-09-24 02:23:36 字数 244 浏览 0 评论 0原文

快问。我刚刚读到,如果您想向 List 模块添加一个函数,您可以使用该函数定义一个新的 List 模块:

module List
  let foo = // ...

这是否具有将 foo 添加到主 List 模块的效果,或者您是否必须显式打开新的List?前者看起来像是 Ruby 的“猴子补丁”;我想后者更像是扩展方法。

(我会尝试一下,但我不靠近编译器。)

Quick question. I just read that if you wanted to add a function to e.g. the List module, you can define a new List module with that function:

module List
  let foo = // ...

Does this have the effect of adding foo to the main List module, or do you have to explicitly open the new List? The former seems like Ruby's "monkey patching"; I guess the latter would be more like extension methods.

(I'd try this out but I'm not near a compiler.)

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

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

发布评论

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

评论(2

坐在坟头思考人生 2024-10-01 02:23:36

它很像命名空间;他们是“开放的”。也就是说,我可能有两个文件或两个程序集将类型贡献到命名空间中,然后如果我使用这些文件或引用这些程序集并说

open ThatNamespace

我看到了所有成员的联合。

It's a lot like namespaces; they're "open". That is, I may have two files or two assemblies that contribute types into a namespace, and then if I use those files or reference those assemblies and say

open ThatNamespace

I see the union of all the members.

痴梦一场 2024-10-01 02:23:36

这与扩展方法类似:

module List =
    let doSomething lst v = lst |> List.nth v

This works in a fashion similar to extension methods:

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