Overview

Similarities

Modules and namespaces allow you to:

  • Organize code into zones of related functionality

  • Avoid name collisions

Differences

Property
Namespace
Module

.NET equivalent

namespace

static class

Type

Top-level

Top-level or local

Contains

Modules, Types

Idem + Values, Functions

Annotable

❌ No

✅ Yes

Scope: Namespaces > Files > Modules

Use a module or a namespace

  1. Either qualify the elements individually

  2. Or import everything with the open keyword

    • placed anywhere before the usage, at the top recommended

    • open Name.Space ≡ C♯ using Name.Space

    • open My.Module ≡ C♯ using static My.Module

// Option 1: Qualify usages
let result1 = Arithmetic.add 5 9

// Option 2: Import the entire module
open Arithmetic
let result2 = add 5 9

Imports order

⚠️ Warning: the order of the imports is taken into account, from top to bottom.

  • This allows shadowing (see below).

  • As a result, reordering imports in an existing file may cause compilation to fail!

  • So, although it's advisable to have imports sorted alphabetically, this isn't always possible. It is therefore advisable to indicate this in the code by means of a comment.

Shadowing at import

Imports are done without name conflicts but need disambiguation:

  • Modules and static classes are merged ✅

  • Types and functions are shadowed ❗ - Last-imported-wins mode: see example below - Import order matters

module IntHelper =
    let add x y = x + y

module FloatHelper =
    let add x y : float = x + y

open IntHelper
open FloatHelper // 👈 IntHelper add is shadowed by FloatHelper add

let result = add 1 2 // 💥 Error FS0001: The type 'float' does not match the type 'int'

Keyword global

Because of Shadowing, we may come across cases where we can't import the right namespace. For example, having done open FsCheck and referenced the FsCheck.Xunit library, when you do open Xunit, we don't import the Xunit namespace from the Xunit library but the FsCheck.Xunit namespace!

💡 We resolve fix issue by doing open global.Xunit.

Last updated

Was this helpful?