liveBook · Manning

Namespace

As explained in F# in Action: A Revised Edition of Get Programming with F#

A namespace is a programming-language construct that groups and qualifies names to organize code and avoid naming collisions.

A namespace logically groups types. It gives related types a shared qualified path, making large codebases easier to navigate and helping distinguish similarly named types. In F#, core .NET types such as DateTime, String, and Int32 are available through System. section 2.4.3 section 6.2

Namespaces form a hierarchy. For example, File belongs to System.IO and ZipFile to System.IO.Compression. A namespace declaration applies to following type declarations until another namespace declaration appears. Types can be referenced with their full path, such as Foo.Order. section 6.2.1

Qualify first, then open

Use a full name when clarity matters or when introducing a dependency: namespace Bar.Baz open Foo type Customer = { LastOrder : Order } Opening Foo lets Order be used directly instead of Foo.Order. This removes repeated qualification, but namespaces still contain types rather than values. Use a module when a group needs functions or values. section 6.2.1 section 6.4

Listing 6.6 Working with namespaces in F#
namespace Foo                     #1
type Order = { Name : string }    #2
 
namespace Bar.Baz                 #3
type Customer =                   #4
    {
        Name : string
        LastOrder : Foo.Order     #5
    }

Compare a root namespace, a nested namespace, and a fully qualified type reference in one small declaration sequence. section 6.2.1

Use namespaces deliberately as an application grows:

  • Start small applications with modules when code needs both types and functions; modules can also be opened and nested. section 6.2.5
  • Introduce namespaces when modules in different files need a shared parent, such as Contoso.WebApi.Orders and Contoso.WebApi.Customers. section 6.2.5
  • Share a namespace across multiple application files, packages, or third-party code when the type hierarchy warrants it. section 6.2.1
  • Open System at the top of a program to use Console directly rather than repeatedly typing System.Console. section 6.2.3

Namespaces are ordinary .NET namespaces when F# code is consumed from C#, so their names and behavior remain familiar across the two languages. That interoperability makes namespaces useful at library and application boundaries, not only inside F# code. section 11.2.1

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains hierarchy, declaration scope, qualification, opening, shared namespaces, and the boundary that namespaces cannot contain values.
Examples
Uses System.DateTime, System.IO, Foo.Order, and opening System for a console-input exercise.
What it assumes
Introduces namespaces through familiar System and DateTime usage before custom namespace design.
What you can do after
Apply it via F# namespace declarations and open directives to organize types and reduce repeated qualification.
How it connects
Connects namespaces with modules, types, .NET, C#, and fully qualified names.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.