F# - 命名空间


命名空间旨在提供一种将一组名称与另一组名称分开的方法。在一个命名空间中声明的类名不会与在另一个命名空间中声明的相同类名冲突。

根据 MSDN 库,命名空间允许您将名称附加到一组程序元素,从而将代码组织到相关功能的区域中。

声明一个命名空间

要在命名空间中组织代码,必须将命名空间声明为文件中的第一个声明。整个文件的内容将成为命名空间的一部分。

namespace [parent-namespaces.]identifier

下面的例子说明了这个概念 -

例子

namespace testing

module testmodule1 =
   let testFunction x y =
      printfn "Values from Module1: %A %A" x y
module testmodule2 =
   let testFunction x y =
      printfn "Values from Module2: %A %A" x y

module 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") 150
Values from Module2: seq [1; 4; 9; 16; ...] 200