无涯教程-F# - 命名空间

39 阅读1分钟

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

声明命名空间

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

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

参考链接

www.learnfk.com/fsharp/fsha…