In F#, a module is a grouping of F# code, such as values, types, and function values. Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.
.
notation.The basic syntax to define a module is as follows.
// Top-level module declaration.
module [accessibility-modifier] [qualified-namespace.]module-name
declarations
// Local module declaration.
module [accessibility-modifier] module-name =
declarations
There are two types of module declarations, depending on whether the whole file is included in the module:
The following code example shows a top-level module that contains all the code up to the end of the file.
module Calculator =
let Add a b = a+b
let Subtract a b = a-b
let Multiply a b = a*b
let Division a b = a/b
To use this code from another file in the same project, you either use qualified names or open the module before using the functions, as shown in the following examples.
module test =
Console.WriteLine(Calculator.Add 8 2)
Console.WriteLine(Calculator.Subtract 8 2)
Console.WriteLine(Calculator.Multiply 8 2)
Console.WriteLine(Calculator.Division 8 2)
You can nest modules, inner modules must be indented as far as outer module declarations to indicate that they are inner modules, not new modules.
The following example shows nested modules.
module A =
let aa = 1
module B =
let bb = 5
But in the following example; module A
is a sibling to module B
, as shown below.
module A =
let aa = 1
module B =
let bb = 5