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
广告

© . All rights reserved.