I've just started learning F# (with little prior experience with .NET) so forgive me for what is probably a very simple question: What the difference between a namespace and a module in F#?
我刚开始学习f#(之前很少使用。net),所以请原谅我有一个很简单的问题:在f#中命名空间和模块有什么区别?
Thanks
谢谢
Dave
戴夫
Edit: Thanks for the answer Brian. That's what I wanted to know. Just a clarification: can you also open a namespace as well (similar to C# using statement)?
编辑:谢谢你的回答,布莱恩。这就是我想知道的。澄清一下:您是否也可以打开一个名称空间(类似于c# using语句)?
1 个解决方案
#1
81
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.
名称空间是一个。net的东西,在许多工业语言中很常见,它只是一种组织框架和避免在不同的库之间命名冲突的方法。您和我都可以定义一个类型“Foo”,并在项目中使用它们,前提是它们在不同的名称空间中(例如,NS1)。Foo和NS2.Foo)。. net中的名称空间包含类型。
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.
模块是f#的东西,它大致类似于“静态类”…它是一个可以保存let绑定的值和函数以及类型的实体(注意名称空间不能直接包含值/函数,名称空间只能包含类型,这些类型本身可以包含值和函数)。模块内的内容可以通过“ModuleName”进行引用。它与名称空间的语法相同,但是f#中的模块也可以“打开”以允许不合格的访问。
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").
(编辑:名称空间也可以类似地打开,但是由于模块可以包含值和函数,所以打开一个模块会更“有趣”,因为您最终会得到值和函数,例如。“cos”是你可以直接使用的名字,而在其他。net语言中,你通常需要对它进行限定。“Math.cos”)。
If you type in code at 'the top level' in F#, this code implicitly goes in a module.
如果您在f#的“顶层”输入代码,那么该代码将隐式地进入模块。
Hope that helps somewhat, it's a pretty open-ended question. :)
希望这能有所帮助,这是一个开放式的问题。:)
#1
81
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.
名称空间是一个。net的东西,在许多工业语言中很常见,它只是一种组织框架和避免在不同的库之间命名冲突的方法。您和我都可以定义一个类型“Foo”,并在项目中使用它们,前提是它们在不同的名称空间中(例如,NS1)。Foo和NS2.Foo)。. net中的名称空间包含类型。
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.
模块是f#的东西,它大致类似于“静态类”…它是一个可以保存let绑定的值和函数以及类型的实体(注意名称空间不能直接包含值/函数,名称空间只能包含类型,这些类型本身可以包含值和函数)。模块内的内容可以通过“ModuleName”进行引用。它与名称空间的语法相同,但是f#中的模块也可以“打开”以允许不合格的访问。
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").
(编辑:名称空间也可以类似地打开,但是由于模块可以包含值和函数,所以打开一个模块会更“有趣”,因为您最终会得到值和函数,例如。“cos”是你可以直接使用的名字,而在其他。net语言中,你通常需要对它进行限定。“Math.cos”)。
If you type in code at 'the top level' in F#, this code implicitly goes in a module.
如果您在f#的“顶层”输入代码,那么该代码将隐式地进入模块。
Hope that helps somewhat, it's a pretty open-ended question. :)
希望这能有所帮助,这是一个开放式的问题。:)