Complex Types
As well as the basic types, fi also allows the following complex types which usually work in conjunction with other types. Complex types can't be used in a literal way and have specific constructors for creating new variables.

Maps

Maps require a key and an element, and can form a large list of mapped elements. Keys must be unique for each entry, and can be used to retrieve a specific entry for a map.
A map key must be one of the following types:
  • string
  • bool
  • int
  • nat
  • bytes
  • mutez
  • timestamp
  • address
  • key_hash/pkh
We define the map type in the following way:
1
storage map[address => nat] balances;
Copied!
This creates a map type variable, with a map key of type address, and a map item of type nat, stored as storage.balances.

Empty Map

An empty map can be defined using the following:
1
let map[int=>string] passages = new map(int, string);
Copied!

Operations

The following operations can be used with maps
  • in(map, map_key) - returns bool if key exists as a map key in the map
  • length(map) - returns cardinal size of the map as a nat
  • get(map, map_key) - returns item with index key. Fails if key doesn't exist (it is advised to use in first)
  • push(map, map_key, map_item) - no return. Pushes item into map at index key. Will add the element if key doesn't exist, and update if it does (acts as an upsert)
  • drop(map, map_key) - no return. Removes the key/element pair from the map
These functions can be used as standalone functions, or as a variable modifier.
1
let map[int => string] textMap = new map(int, string);
2
push(textMap, int 1, "One");
3
textMap.push(int 2, "One"); // Same as above
Copied!

Big Map

Big maps work the same as normal maps, but have a few restrictions:
  1. 1.
    Big maps can only be declared as a storage variable
  2. 2.
    Only one big_map can exist for any given contract
  3. 3.
    The length() function doesn't work with big_maps
  4. 4.
    Big maps can't be constructed
We define the map type in the following way:
1
storage bmap[address => nat] balances;
Copied!
All functions, other then length, are applicable for big maps.

Lists

Lists are non-indexed arrays of a specified type, and are defined as follows:
1
let string[] names;
Copied!
This creates a list of the specified type - in this instance a list of strings, which can be accessed via storage.names.

Empty list

An empty map can be defined using the following:
1
let string[] names = new list(string);
Copied!

Operations

The following operations are available for lists:
  • length(list) - returns cardinal size of a list as a nat
  • push(list, item) - pushes an item to a list
  • pop(list) - removes the last item from a list and returns it
These functions can be used as standalone functions, or as a variable modifier.
1
let string[] names = new list(string);
2
push(names, "John");
3
names.push("Bill"); // Same as above
Copied!

Set

Sets are similar to lists as we store a single type of data per entry, and can be declared as follows:
1
storage set[nat] numbers;
Copied!
The set item type must be one of the following types (same as the map key):
  • string
  • bool
  • int
  • nat
  • bytes
  • mutez
  • timestamp
  • address
  • key_hash/pkh

Operations

The following operations are available for lists:
  • length(set) - returns cardinal size of a set as a nat
  • push(set, item) - pushes an item to a set
  • in(set, item) - returns bool if item exists in the set
  • drop(set, item) - no return. Removes the item from a set
These functions can be used as standalone functions, or as a variable modifier.
1
let set[string] names = new set(string);
2
push(names, "John");
3
names.push("Bill");
Copied!

Contract

The contract type is the typed version of an address. Michelson contract types must also specify the type of the input parameter.
1
let contract[unit] implicit = to_contract(address "tz1...");
Copied!

Option

Optional values can be set and then used within expressions. These values either hold "some" value, or "none". Optional values are defined by adding a question mark before another type:
1
let ?nat optional = to_optional(nat 100);
Copied!
We can convert any existing value to an optional value using the to_optional cast, or we can use the none function to create an optional variable with no value.
1
let ?bool test = none(bool);
2
if (isset(test) == false) {
3
#This will execute
4
}
Copied!
Last modified 2yr ago