DFINITY Motoko Programming Language

The DFINITY Canister SDK is alpha software from our final development branch that will make it into production. Please be aware that at this time it is less developed than previously-demonstrated prototypes and you should check back regularly to try new features.

Until the production release of the SDK, the Motoko language is under constant development and all features are subject to final security review.

Enjoy!

The Motoko programming language is a new, modern, type-sound language designed for developers who want to build the next generation of apps and services to run directly on the internet. Motoko is specifically designed to support the unique features of the Internet Computer and to provide a familiar yet robust programming environment. As a new language Motoko is constantly evolving with support for new features and improvements. Keep checking back for updates and for the announcement of the Motoko becoming available as an open source project.

Native canister support

Motoko has native support for Internet Computer software canisters, which are expressed as actors, autonomous objects that encapsulate their state and communicate through asynchronous messages.

actor Counter {
  var value = 0;
  public func inc() : async Nat {
    value += 1;
    return value;
  };
}

Code sequentially in direct style

On the Internet Computer, software canisters call into other canisters asynchronously, but Motoko enables you to program your systems sequentially in direct style. Asynchronous messages are function calls that return a future, and the await constructs allows chaining calls as if they were synchronous.

actor Factorial {
  var last = 1;
  public func next() : async Nat {
    last *= (await Counter.inc());
    return last;
  }
}

Modern type system

Motoko has been designed to be intuitive to those familiar with Javascript and other popular languages, but offers modern features such as sound structural types, generics, variant types, and checked pattern matching.

type Tree<T> = {
  #leaf : T;
  #branch : {left : Tree<T>; right : Tree<T>};
};

func iterTree<T>(tree : Tree<T>, f : T -> ()) {
  switch (tree) {
    case (#leaf(x)) { f(x) };
    case (#branch{left; right}) {
      iterTree<T>(left, f);
      iterTree<T>(right, f);
    };
  }
}

Autogenerated IDL files

The SDK exports your interface definition, so other canisters, browser resident code and smart phone apps that have permission can call into your functions. The Motoko compiler can also read and write interface definition files, allowing Motoko to seamlessly interact with canisters programmed in other languages.

service Counter : {
  inc : () -> (nat);
  reset : () -> ();
}

Orthogonal persistence

The Internet Computer persists the memory pages in which your canister runs. Thus the state of an actor and all its memory data structures survive indefinitely, they do not need to be “saved” explicitly. Motoko provides numerous features to help you leverage this environment, including language features that allow your heap to self-migrate when you upgrade the software of a canister.

import Dict = "mo:dict";

actor Registry {
  type Id = Nat64;
  let dict = Dict<Id>();
  public func register(name : Text, id : Id) {
    dict.insert<Id>(name, id);
  };
  public func lookup(name : Text, id : Id) : async ?Id {
    dict.lookup<Id>(name);
  };
}

And more …​

Motoko provides many other cool features, including deep support for WebAssembly that allows modules written in other languages to be linked into your canisters. We are also working on adding Rust support to the SDK.