Composed Types

Option

Options represents a value that could be defined Some(value) or not None.

const a: nat option = None;
const b: nat option = Some(12n);

The option type offers some helpers:

const c: bool = isNone(a); // returns true if a is None
const d: bool = isSome(a); // returns true if a is Some(_)
const e: nat = getSome(b): // extract the value (if any) or fail

List

List type represents list of same-type elements; a list can be declared as follow:

let l: nat list = (List.empty(): nat list); // empty list
let l1: nat list = [12n, 13n]; // literal list
let ls: nat = l.size();
let lh: nat = l.head();
let lt: nat list = l.tail();
let l2: nat list = l.prepend(14n);
let l3: nat list = l.mapWith((a: nat) => (a * 2n));

prepend, mapWith and filter are applied in-place if used over storage fields. Their types are unit instead of nat list. To apply as expression, surround the storage field with copy().

Set

Sets are list of different elements of the same-type.

update are applied in-place if used over storage fields. Their types are unit instead of nat set. To apply as expression, surround the storage field with copy().

Tuple

A tuple is a pair, triple, etc of different types; you can define a tuple as follow:

Record

Map & Big_map

('a, 'b) Map and ('a, 'b) Big_map are collections of 'b indexed by a key 'a; 'a should be a comparable.

update, remove, mapWith, fold, filter are applied in-place if used over storage fields. Their types are unit instead of nat set. To apply as expression, surround the storage field with copy().

Map

Big_map

Lambda

Lambda type represent anonymous functions.

In-place modification

While calling some of the composed type specific functions, if the left-hand is storage field, the modification will be automatically assigned to the field storage.

To avoid this behaviour, you can use the copy helper function.

Last updated

Was this helpful?