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

Method Calls and Field Access

Cambrian uses dot notation for both field access on records and method calls on built-in types. Bracket notation provides indexed access to maps and collections.

Field Access

Record fields are accessed with the dot operator:

let seller = listing.seller;
let is_live = listing.active;
let max = config.limits.max_deposit;

Member state variables are accessed by name within member transforms, where clauses, macros, and route actions:

where m_balance >= amount : throw 100

Map Access

HashMap values are accessed with bracket notation:

let bal = m_balances[account];

Bracket access panics if the key does not exist. For safe access, use the .get() method which returns an Option<V>:

let bal = m_balances.get(account).unwrap_or(0);

Method Calls

Built-in types provide methods that are called with dot notation:

Vec Methods

let size = m_items.len();
let updated = m_items.push(new_item);
let first = m_items.get(0);
let empty = m_items.is_empty();

HashMap Methods

let value = m_map.get(key);
let updated = m_map.set(key, value);
let has_key = m_map.exists(key);
let without = m_map.remove(key);
let size = m_map.len();

Option Methods

let val = opt.unwrap();
let safe_val = opt.unwrap_or(default);
let present = opt.is_some();
let absent = opt.is_none();

String Methods

let size = name.len();

Method Chaining

Methods that return the collection type can be chained:

m_balances
    .set(from, from_bal - amount)
    .set(to, to_bal + amount)

This pattern is common in member transforms where multiple map updates happen atomically.

Type Casts

The as keyword casts a value to a different type:

let wide = narrow_val as u64;
let big = amount as U256;

Casts are necessary when performing arithmetic across different integer widths. The compiler requires explicit casts – implicit widening does not occur in most positions. See Arithmetic and Logic for how this interacts with checked and wrapping arithmetic.

Common Cast Patterns

// Narrowing (may truncate)
let byte_val = large_num as u8;

// Widening (always safe)
let big_val = small_num as u128;

// Between signed and unsigned
let signed = unsigned_val as i64;

Combining Access Patterns

Field access, method calls, and bracket access can be combined freely:

let bidder_balance = m_auctions[auction_id].highest_bid.amount;

let winner = m_results.get(round).unwrap_or(default_result).winner;

Pure Function Calls

Top-level pure functions are called by name (without dot notation):

let fee = compute_fee(amount, rate);
let clamped = clamp(value, min, max);

Macro Invocation

Entity-scoped macros are called with the @ prefix:

where @is_owner() : throw 100

Macros look like function calls but can access entity state. See Macros for details.