A namespace lets you organize code into related functionality areas by enabling you to attach a name to a grouping of F# program elements.
The basic syntax of the namespace is as follows.
namespace [rec] [parent-namespaces.]identifier
The following example shows a code file that declares a namespace Calculator
containing two modules.
namespace Calculator
module Arthimetic =
let Add a b = a+b
let Subtract a b = a-b
let Multiply a b = a*b
let Division a b = a/b
module test =
System.Console.WriteLine(Arthimetic.Add 8 2)
System.Console.WriteLine(Arthimetic.Subtract 8 2)
System.Console.WriteLine(Arthimetic.Multiply 8 2)
System.Console.WriteLine(Arthimetic.Division 8 2)
If the entire contents of the file are in one module, you can also declare namespaces implicitly by using the module
keyword and providing the new namespace name as the fully qualified module name.
The following example shows a code file that declares a namespace Calculator
and a module Arthimetic
, which contains a function.
module Calculator.Arithmetic
let Add a b = a+b
let Subtract a b = a-b
let Multiply a b = a*b
let Division a b = a/b
The above example is equivalent to the following code.
namespace Calculator
module Arithmetic =
let Add a b = a+b
let Subtract a b = a-b
let Multiply a b = a*b
let Division a b = a/b
When you create a nested namespace, you must fully qualify it. Otherwise, you create a new top-level namespace. Indentation is ignored in namespace declarations.
The following example shows how to declare a nested namespace.
namespace Outer
// Full name: Outer.A
type A() =
member this.X(x) = x + 1
// Fully qualify any nested namespaces.
namespace Outer.Inner
// Full name: Outer.Inner.B
type B() =
member this.Y = "y"