简体   繁体   English

F#中命名空间和模块之间的区别是什么?

[英]What the difference between a namespace and a module in F#?

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. 编辑:谢谢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语句)?

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. 命名空间是一种.Net的东西,在许多工业级语言中很常见,只是一种组织框架和避免不同库之间命名冲突的方法。 Both you and I can define a type "Foo" and use them both in a project, provided they are in different namespaces (eg NS1.Foo and NS2.Foo). 您和我都可以定义类型“Foo”并在项目中使用它们,只要它们位于不同的名称空间(例如NS1.Foo和NS2.Foo)。 Namespaces in .Net contain types. .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). 模块是F#的东西,它大致类似于“静态类”......它是一个可以保存let-bound值和函数的实体,以及类型(请注意,名称空间不能直接包含值/函数,名称空间只能包含类型,它们本身可以包含值和函数)。 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, eg 模块内部的东西可以通过“ModuleName.Thing”引用,这与命名空间的语法相同,但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, eg "cos", being names you can use directly, whereas in other .Net languages you'd typically always have to qualify it, eg "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. 希望有所帮助,这是一个非常开放的问题。 :) :)

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

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