简体   繁体   English

F#中模块和命名空间之间的差异

[英]DIfference between Modules and Namespaces in F#

I have a problem in understanding the exact difference between modules and namespaces in F# and when using one or the other. 我在理解F#中的模块和命名空间之间的确切区别以及使用其中一个时遇到了问题。 Well, they both are considered in order to incapsulate code and define a hierarchy to get our projects well organized. 好吧,它们都被认为是为了封装代码并定义层次结构以使我们的项目井井有条。

Modules have many features: they can contain values, types of all kinds and these elements can be defined public , protected or internal too. 模块具有许多功能:它们可以包含值,各种类型,这些元素也可以定义为publicprotectedinternal

But, when using modules? 但是,在使用模块时?

I understood also that modules are finally mapped as classes in MSIL (Intermediate Language). 我也理解模块最终被映射为MSIL(中间语言)中的类。 So a module is a class, is a type.... My doubts' magnitude improve.... 所以一个模块是一个类,是一个类型....我的怀疑'幅度提高....

When using modules??? 使用模块时??? Are they useful? 它们有用吗?

D. Syme also argues that modules are extensible, so I assume they are like namespaces from this point of view. D. Syme还认为模块是可扩展的,所以我认为从这个角度看它们就像命名空间一样。

I cannot understand the reason to use them. 我无法理解使用它们的原因。

Thankyou 谢谢

One major difference: 一个主要区别:

.Net namespaces cannot hold values (let definitions) while modules can. .Net名称空间不能保存 (让定义)而模块可以。

In the IL/Bytecode level, a module is compiled to a .net class, not a .net namespace. 在IL / Bytecode级别,模块被编译为.net类,而不是.net名称空间。

When to use modules? 什么时候使用模块?

For a small and specific task, F# and other FPs usually follow the pattern of bottom up programming: you decompose your task into a set of small functions and then you group these functions into a module. 对于一个小而具体的任务,F#和其他FP通常遵循自下而上编程的模式:将任务分解为一组小函数,然后将这些函数分组到一个模块中。

The answer is that it is just so natural to use a module to group a set of related functions and other F# values and types together. 答案是,使用模块将一组相关函数和其他F#值和类型组合在一起是非常自然的。

While a namespace is used to group bigger things: eg all classes for Matrix operations. 虽然命名空间用于分组更大的东西:例如Matrix操作的所有类。

Module is not a static class (in C# sense) Modules can hold special F# values, eg curried functions; 模块不是静态类(在C#意义上)模块可以保存特殊的F#值,例如curried函数; while a static class cannot. 虽然静态类不能。

As Yin Zhu says, modules can hold values. 正如尹朱所说,模块可以保持价值。 And you can open modules like namespaces. 您可以打开名称空间等模块。 These two features together are why in F# you can do things like 这两个功能在一起就是为什么在F#中你可以做的事情

let z = max x y

whereas in a language like C# you'd always have to say something like 而在像C#这样的语言中,你总是要说些什么

var z = Math.Max(x,y)
//      ^^^^^   can't call methods from elsewhere without a qualifier

and use a qualified name ( SomeClass.Method ) rather than just a name ( letBoundFunction ). 并使用限定名称( SomeClass.Method )而不仅仅是名称( letBoundFunction )。 So you can use modules when you want people to be able to open you module Foo and call bar by just saying bar rather than Foo.bar everywhere. 所以你可以使用模块,当你希望人们能够打开你的模块Foo并通过只是说bar而不是Foo.bar调用bar (This is especially useful for operators, eg if you define a bunch of user-defined operators (such as +++ or whatnot) in a library, by putting them in a module, people can open the module and then just use eg x +++ y rather than cumbersome stuff like (Foo.+++) xy or whatever.) (这对于操作员特别有用,例如,如果您在库中定义了一堆用户定义的操作符(例如+++或whatnot),通过将它们放在模块中,人们可以打开模块然后只使用例如x +++ y而不是像(Foo.+++) xy或其他任何繁琐的东西。)

Note that while F# puts code in a .fs file in a module (with the name of the file) by default, you can change this by having the first code in the file be a namespace declaration, eg 请注意,虽然默认情况下F#将代码放在模块中的.fs文件中(带有文件名),但您可以通过将文件中的第一个代码作为名称空间声明来更改此项,例如

namespace Yadda
// declare some types or whatnot, they go in namespace Yadda

or by declaring your own module 或者通过声明自己的模块

module Blah
// stuff goes in Blah module

at the top of the file. 在文件的顶部。

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

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