Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generic Types

Parameterized types for collections, optionals, and typed entity addresses.

Vec<T>

Dynamic ordered sequence:

m_participants: Vec<address> {
    in register(addr) => m_participants.push(addr)
}
MethodDescription
v.len()Length
v.push(elem)Append (as the tail of a Vec member transform on EVM)
v.get(index)Option<T>
v.is_empty()Emptiness
let empty: Vec<u64> = array();
let nums: Vec<u64> = array(1, 2, 3);

HashMap<K, V>

Key-value map (keys are hashable primitives / address):

m_balances: HashMap<address, U256> {
    in transfer(from, to, amount) => {
        let sender_bal = m_balances.get(from).unwrap_or(0);
        let receiver_bal = m_balances.get(to).unwrap_or(0);
        m_balances
            .set(from, sender_bal - amount)
            .set(to, receiver_bal + amount)
    }
}
MethodDescription
m.get(key)Option<V>
m.set / m.update / m.insertWrite
m.exists(key)Presence
m.remove(key)Delete
m[key]Direct access
let scores: HashMap<address, u64> = {};

Option<T>

some(value) / none:

let result = match m_data.get(key) {
    some(val) => val * 2,
    none => 0
};

Address<Entity>

Typed wrapper around address that carries the target entity at compile time. Named messages require a typed destination (V23 on a plain address). Plain value transfers (~> dest with { value }) may use untyped address.

m_root: Address<RootToken>     // named sends OK
m_backup: address              // value transfer only

Predicting addresses (EVM)

On the EVM target, compute a destination without a manual factory:

macro pairAddr(token0: address, token1: address) -> Address<UniswapV2Pair> = {
    UniswapV2Pair.address(token0, token1)
}

routes {
    notify(token0: address, token1: address) => [
        Ping() ~> UniswapV2Pair.address(token0, token1)
    ]
}

Entity.address(args) and the addressOf(...) spelling lower to the same CREATE2 expression on EVM. Singletons use Entity.address() with no arguments. See Deterministic Addresses and Deploy.

FeatureAddress<Entity>address
Named sendAllowedV23
Plain value transferAllowedAllowed
Compile-time route checksYesNo