Modules in an F# project
April 9, 2017 Leave a comment
F# supports grouping related code into namespaces and modules. Namespaces can only contain type declarations. They cannot contain values i.e. elements that are declared with the “let” keyword. Modules on the other hand can handle both types and values. Modules can also contain other modules, i.e. they can be nested.
Here’s a simple module with a type and a function:
module com.mycompany.domain.modules type Address = { street: string; city: string; number: int; } with member this.evenNumber = this.number % 2 = 0 let sumFunction x y = x + y
Here’s how we can reference and use it from Program.fs using the “open” keyword:
namespace com.mycompany.entry open System open com.mycompany.domain.modules module Main = [<EntryPoint>] let main args = let myAddress = {street = "Rue Bourbon"; city = "New Orleans"; number = 14} let isEven = myAddress.evenNumber printfn "Street name: %s, number is even: %b" myAddress.street isEven let sum = sumFunction 12 30 let keepConsoleWindowOpen = Console.ReadKey() 0
This will print…
Street name: Rue Bourbon, number is even: true
…in the console window and “sum” will of course be 42.
We can add a namespace to a module. The module name must be simple, i.e. not a path and followed by an equal sign. Furthermore, everything that’s included in the module must be indented:
namespace com.mycompany.domains.main module AddressModule = type Address = { street: string; city: string; number: int; } with member this.evenNumber = this.number % 2 = 0 let sumFunction x y = x + y
The full reference path in Program.cs will be com.mycompany.domains.main.AddressModule:
namespace com.mycompany.entry open System open com.mycompany.domains.main.AddressModule module Main = [<EntryPoint>] let main args = let myAddress = {street = "Rue Bourbon"; city = "New Orleans"; number = 14} let isEven = myAddress.evenNumber printfn "Street name: %s, number is even: %b" myAddress.street isEven let sum = sumFunction 12 30 let keepConsoleWindowOpen = Console.ReadKey() 0
A namespace can include multiple modules:
namespace com.mycompany.domains.main module AddressModule = type Address = { street: string; city: string; number: int; } with member this.evenNumber = this.number % 2 = 0 let sumFunction x y = x + y module BookModule = type Book = { title: string; numberOfPages:int; author: string } with member this.takesLongTimeToRead = this.numberOfPages > 500
A module can include submodules but pay attention to the indentation:
namespace com.mycompany.domains.main module AddressModule = type Address = { street: string; city: string; number: int; } with member this.evenNumber = this.number % 2 = 0 let sumFunction x y = x + y module OrderModule = type Order (product:string, value: int, address:Address) = member this.Product = product member this.Value = value member this.Address = address
The full reference path to the OrderModule will be…
open com.mycompany.domains.main.AddressModule.OrderModule
…, i.e. the namespace, followed by the top module name and then by the submodule name.
View all F# related articles here.