F# 模块可以进行猴子修补吗?
快问。我刚刚读到,如果您想向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它很像命名空间;他们是“开放的”。也就是说,我可能有两个文件或两个程序集将类型贡献到命名空间中,然后如果我使用这些文件或引用这些程序集并说
我看到了所有成员的联合。
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
I see the union of all the members.
这与扩展方法类似:
This works in a fashion similar to extension methods: