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

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

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?