The Base Library

The Motoko Base Library is a collection of modules with basic functionality for working with primitive types, utilities, data structures and other functionality.

Most modules define a public type that is associated with the core use of the module.

The Base Library also includes IC system APIs, some of which are still experimental (June 2023). We will visit them later in this book.

Importing from the Base Library

An import from the base library looks like this:

import P "mo:base/Principal";

We imported the Principle module. The P in the example is an arbitrary alias (name) that we choose to reference our module. The import path starts with mo:base/ followed by the file name where the module is defined (without the .mo extension).

The file name and our chosen module name do not have to be the same (like in the example above). It is a convention though to use the same name:

import Principal "mo:base/Principal";

Our imported module, now named Principal, has a module type module { ... } with public fields. It exposes several public types and public functions that we can now reference by using our alias:

let p : Principal = Principal.fromText("2vxsx-fae");

Note the two meanings of Principal:

  • The type annotation for variable p uses the always available primitive type Principal. This type does not have to be imported.

  • We used the .fromText() method from the Principal module (public functions in modules or in objects or classes are often called methods) by referencing it through our chosen module name Principal.