命名空间旨在提供一种方法,使一组名称与另一组名称分开.在一个名称空间中声明的类名称不会与在另一个名称空间中声明的相同类名称冲突.
根据MSDN库,名称空间允许您将代码组织到通过允许您将名称附加到一组程序元素的相关功能区域.
声明命名空间
在命名空间中组织代码,您必须将名称空间声明为文件中的第一个声明.然后整个文件的内容成为命名空间的一部分.
namespace [parent-namespaces.]identifier
以下示例说明概念 :
示例
namespace testingmodule testmodule1 = let testFunction x y = printfn "Values from Module1: %A %A" x ymodule testmodule2 = let testFunction x y = printfn "Values from Module2: %A %A" x ymodule usermodule = do testmodule1.testFunction ( "one", "two", "three" ) 150 testmodule2.testFunction (seq { for i in 1 .. 10 do yield i * i }) 200
编译并执行程序时,它会产生以下输出 :
Values from Module1: ("one", "two", "three") 150Values from Module2: seq [1; 4; 9; 16; ...] 200