Developer Resources
Introduction to the Motoko Programming Language
Find out more about Motoko, the native programming language of the Internet Computer
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.
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:
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:
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.
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.
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.
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:
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
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 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.