Developer Resources

Introduction to the Motoko Programming Language

Find out more about Motoko, the native programming language of the Internet Computer

IC Academy » Developer Resources » Introduction to the Motoko Programming Language

Discover Motoko

Learn More about the Internet Computer’s Native Language

Introduction to Motoko

The native programming language of the Internet Computer is called Motoko. It directly supports the IC’s programming model, empowering developers to seamlessly build applications and use the ecosystem’s more unusual features.

What is Motoko?

Motoko is the native programming language of the Internet Computer. It is a type-safe and modern programming language designed for developers who wish to build the next generation of dApps on the Internet Computer. Motoko enables developers to use the unique features of the IC.

Motoko is a new language that is continuously evolving with each new release of the DFINITY Canister SDK.

The Dfinity Foundation has released the Motoko compiler, its documentation, and other tooling under the Apache 2.0 license as open source.

Properties of Motoko

Let’s have a look at the properties of the programming language:

Strongly typed language

Strict typing rules at compile time

Actor-based computation

Actors are treated as the universal primitive of concurrent computation

Orthogonal persistence

Motoko natively support orthogonal persistence

Asynchronous message passing

Motoko has built-in support for asynchronous message passing

Additionally, every construct within Motoko is an expression. Motoko has closures and variant types. Another property of Motoko is that it has statistically checked pattern matching as well as garbage collection. Lastly, its flexible type system ensures the absence of errors, such as crashes, data misinterpretation, and undefined behavior.

Motoko’s Productivity and Safety Features

The native programming language of the Internet Computer features several important attributes which increase its safety and productivity:

The Candid interface definition language of the Internet Computer is transparently employed for messaging. High-level, typed, and cross-language interoperability is enabled by wire format.

Actors – Central Feature of Motoko

The direct support for actors in Motoko’s syntax and type system is a central feature of the programming language. An actor in Motoko encapsulates a private state and has a set of methods by means of which it can process messages.

All message sends in Motoko are asynchronous, meaning that there are no results for actor methods.

Actors receive messages sequentially. As a result, there is an implicit message queue. Additionally, methods are being executed atomically. This is even the case when messages are sent concurrently.

Every application in Motoko resembles an actor (or several actors). These actors, in turn, are big asynchronous objects that are being compiled into a Wasm module. With the inherent memory features of Wasm, such actors can manage up to 4GB of internal state. This figure can be further enlarged by linking multiple Wasm modules.

Futures

Aside from actors, Motoko adopts another important concept of programming language research: futures – also known as promises. Futures materialize in the programming language as “async values,” i.e. values of the async t type. This type is produced by expressions that are prefixing the async keyword. As a result, an async expression may be used for a function body. This naturally replaces and generalizes the “async function” which after languages use.

In combination with futures, actor methods can now have results – under the condition that they are futures. Similar to other modern programming languages, futures can await getting their value – with the limitation that this happens inside another async expression. This is similar to async/await monads other modern programming languages are using.

Futures are being implemented by the Motoko compiler via a traditional Continuation Passing Style (CPS) transformation. Each await point is turned by the CPS transformation into a separate Wasm function. In addition to the closure environment, this represents the continuation of the await.

CPS is double-barreled, meaning every message can have a failure reply.

An actor does not get blocked when waiting for a reply. Instead, it can receive other messages despite waiting for a reply.

Persistence

Motoko empowers developers to make use of the Internet Computer’s blockchain technology without learning a new type of computing. In practice, this means the following is excluded in Motoko:

  • No observable block or block height
  • No explicit constructs on the blockchain for updating state
  • No API for persistent storage data writing
  • No files or databases (can be emulated as library, however)

Instead of the aforementioned features, another old computer science idea is implemented on the IC:

The Internet Computer implements orthogonal persistence, giving programs the illusion of running “forever” with their memory staying alive until taken down

In practice, orthogonal persistence helps developers to avoid having to save all of their data between messages. It also allows them to avoid having to deal with files or databases. Instead, the data stored in the program is still available when the next message arrives.

In Conclusion

The Internet computer is running on Wasm, which makes Motoko one of many options developers can choose from for creating an application.

Rust is also provided on the IC and other languages will be made available in the coming future.

Each different programming language uniformly compiles to Wasm-powered canisters. This enables canisters to freely communicate with other canisters independently of their source language.

The Internet Computer’s generic interface definition language (IDL), called Candid, enables aforementioned interoperability. It serves as the lingua franca that is used for communication on the Internet Computer. As such, it is independent of Motoko.

The Motoko compiler can map interface descriptions for actor imports/exports and maps them with corresponding Motoko types. Additionally, the compiler generates the Wasm code to serialize/deserialize argument data for each message.

Discover Motoko’s Features

Learn more about the developer productivity features that Motoko offers to developers

Developer productivity features

Using the Motoko Base Library

Learn how you can use the Motoko base modules to minimize built-in types and operations

More about the base library