📜  F#-命名空间

📅  最后修改于: 2020-11-21 07:02:38             🧑  作者: Mango


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

根据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