F # 中的名称空间和模块有什么区别?

我刚刚开始学习 F # (之前没有什么经验。NET) ,所以请原谅我可能是一个非常简单的问题: F # 中的名称空间和模块之间的区别是什么?

谢谢

戴夫

编辑: 谢谢你的回答,布莱恩。这就是我想知道的。 只是澄清一下: 您是否也可以打开一个名称空间(类似于 C # using 语句) ?

16409 次浏览

A namespace is a .Net thing, common in many industrial-strength languages, just a way to organize frameworks and avoid naming conflicts among different libraries. Both you and I can define a type "Foo" and use them both in a project, provided they are in different namespaces (e.g. NS1.Foo and NS2.Foo). Namespaces in .Net contain types.

A module is an F# thing, it is roughly analogous to a "static class"... it is an entity that can hold let-bound values and functions, as well as types (note that namespaces cannot directly contain values/functions, namespaces can only contain types, which themselves can contain values and functions). Things inside a module can be referenced via "ModuleName.Thing", which is the same syntax as for namespaces, but modules in F# can also be 'opened' to allow for unqualified access, e.g.

open ModuleName
...
Thing  // rather than ModuleName.Thing

(EDIT: Namespaces can also similarly be opened, but the fact that modules can contain values and functions makes opening a module more 'interesting', in that you can wind up with values and functions, e.g. "cos", being names you can use directly, whereas in other .Net languages you'd typically always have to qualify it, e.g. "Math.cos").

If you type in code at 'the top level' in F#, this code implicitly goes in a module.

Hope that helps somewhat, it's a pretty open-ended question. :)