F#中的模块v.名称空间 [英] Modules v. namespaces in F#
问题描述
我有一个用F#编写的库,供C#和F#使用.该库在模块 bar
中,在命名空间 random
中的类 foo
中定义了一个类:
I've got a library written in F#, consumed by C# and F#. this library defines a class foo
, in module bar
, in namespace random
:
#light
namespace random
module bar
type foo() = ...
现在,当我使用类型 random.foo.bar
时,C#intellisense将其视为类型为 bar
的类型,嵌套在类型 random.foo 代码>.
Now, when I go to consume type random.foo.bar
, C# intellisense sees it as type bar
, nested in type random.foo
.
问题是这样的:C#将外部使用的代码定义到模块而不是名称空间中是否有优势?我知道模块是对函数进行分组的好方法,但是类呢?
The question is this: Is there an advantage to C# to define externally-consumable code into modules, rather than namespaces? I understand that modules are a good way to group functions, but what about classes?
推荐答案
如果要发布F#组件以供其他.Net语言使用,则应避免使用公共接口中的模块,并坚持使用包含类,结构的名称空间和枚举.
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类的F#组件的内部"实现细节.)
(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.)
(另请参见此问题讨论名称空间和模块之间的技术区别".这个问题和我在上面的回答更多地是关于故意区别",例如何时选择使用它们.)
(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' e.g. when you would choose to use each.)
这篇关于F#中的模块v.名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!