简体   繁体   English

模块v。F#中的命名空间

[英]Modules v. namespaces in F#

I've got a library written in F#, consumed by C# and F#. 我有一个用F#编写的库,由C#和F#使用。 this library defines a class foo , in module bar , in namespace random : 这个库在模块bar定义一个类foo ,在命名空间random

#light
namespace random

module bar

type foo() = ...

Now, when I go to consume type random.foo.bar , C# intellisense sees it as type bar , nested in type random.foo . 现在,当我去使用random.foo.bar类型random.foo.bar ,C#intellisense将其视为类型bar ,嵌套在random.foo类型中。

The question is this: Is there an advantage to C# to define externally-consumable code into modules, rather than namespaces? 问题是:C#将外部可消耗代码定义到模块而不是命名空间中是否有优势? I understand that modules are a good way to group functions, but what about classes? 我知道模块是分组函数的好方法,但是类呢?

If you're publishing F# components for consumption from other .Net languages, then you should avoid modules in the public interfaces, and stick to namespaces containing classes, structs, and enums. 如果您要发布F#组件以供其他.Net语言使用,那么您应该避免使用公共接口中的模块,并坚持使用包含类,结构和枚举的名称空间。

(Modules are a handy way either to publish values, functions and types among F#-only components, or as 'internal' implementation details of an F# component that publishes .Net classes.) (模块是在F#-only组件中发布值,函数和类型,或者作为发布.Net类的F#组件的“内部”实现细节的便捷方式。)

(Do see also this question for a discussion of the 'technical distinction' between namespaces and modules. This question and my answer above are more about the 'intentional differences' eg when you would choose to use each.) (请参阅这个问题 ,讨论命名空间和模块之间的“技术区别”。这个问题和我上面的答案更多地是关于'故意差异',例如当你选择使用每个时。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM