Shared Types

Incoming and outgoing data are defined by the argument types and return types of public shared functions inside actors. Incoming and outgoing data types are restricted to a subset of available Motoko types, called shared types.

Shared types are always immutable types or data structures composed of immutable types (like records, objects and variants).

Shared types get their name from being sharable with the outside world, that is the wider internet beyond the actors running in canisters on the Internet Computer.

Shared Types in Public Shared Functions

Only shared types are allowed for arguments and return values of public shared functions. We give examples of a custom public shared function type called SharedFunction to illustrate shared types. Recall that a public shared function type includes the shared and async keywords.

Here are the most important shared types in Motoko.

Shared Primitive Types

All primitive types (except the Error type) are shared.

type SharedFunction = shared Nat -> async Text;

The argument of type Nat and the return value of type Text are shared types.

Shared Option Types

Any shared type in Motoko can be turned into an Option type which remains shared.

type SharedFunction = shared ?Principal -> async ?Bool;

The argument of type ?Principal and the return value of type ?Bool are shared types.

Shared Tuple Types

Tuples consisting of shared types are also shared, that is they are shared tuple types.

type SharedFunction = shared (Nat, Int, Float) -> async (Principal, Text);

The argument (Nat, Int, Float) and the return value (Principal, Text) are shared tuple types.

Shared Immutable Array Types

Immutable arrays consisting of shared types are also shared, that is they are shared immutable array types.

type SharedFunction = shared [Int] -> async [Nat];

The types [Int] and [Nat] are shared immutable array types.

Shared Variant Types

Variant types that have shared associated types are also shared.

type GenderAge = {
    #Male : Nat;
    #Female : Nat;
};

type SharedFunction = shared GenderAge -> async GenderAge;

The variant type GenderAge is a shared type because Nat is also shared.

Shared Object Types

Object types that have shared field types are also shared.

type User = {
    id : Principal;
    genderAge : GenderAge;
};

type SharedFunction = shared User -> async User;

Object type User is a shared type because Principal and GenderAge are also shared types.

Shared Function Types

Shared function types are also shared types. This example shows a shared public function that has another shared public function as its argument and return type.

type CheckBalance = shared () -> async Nat;

type SharedFunction = shared CheckBalance -> async CheckBalance;

CheckBalance is a shared type because it is the type of a public shared function.

Shared Actor Types

All actor types are shared types.

type Account = actor {
    checkBalance : shared () -> async Nat;
};

type SharedFunction = shared Account -> async Account;

Account is a shared type because it is the type of an actor.