目录

F# - 命名空间( Namespaces)

namespace旨在提供一种方法来保持一组名称与另一组名称分离。 在一个名称空间中声明的类名称不会与在另一个名称空间中声明的相同类名称冲突。

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

声明命名空间

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

namespace [parent-namespaces.]identifier

以下示例说明了这一概念 -

例子 (Example)

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
<上一篇.F# - Modules
F# - 快速指南.下一篇>
↑回到顶部↑
WIKI教程 @2018