Method Syntax, Macros & Modern Language Features

Sui Move 2024: any function whose first parameter's type is defined in the same module can be called with dot syntax:

22.1 Method (receiver) syntax

Sui Move 2024: any function whose first parameter's type is defined in the same module can be called with dot syntax:

The receiver is auto-borrowed (& or &mut) as needed. Built-in types get methods from their defining modules: v.push_back(1), ctx.sender(), coin.value(), id.delete().

Aptos Move 2: receiver-style calls work when the first parameter is named self:

module shop::item;

public struct Item has key, store { id: UID, price: u64 }

public fun price(self: &Item): u64 { self.price }
public fun set_price(self: &mut Item, p: u64) { self.price = p }

// elsewhere:
// let p = item.price();         // same as item::price(&item)
// item.set_price(10);           // auto-borrows &mut
public fun price(self: &Item): u64 { self.price }
// item.price()

22.2 use fun aliases (Sui)

use fun my_utils::to_cents as u64.cents;    // add a method to u64 within this module
public use fun item_price as Item.cost;     // export a method alias with the type

22.3 Index syntax

Sui lets custom types opt in with #[syntax(index)] on borrow functions (used by Table, VecMap, etc.).

let x = v[2];                    // vector indexing — both dialects (Sui 2024, Aptos Move 2)
let r = &mut v[0];
// Aptos Move 2: resource indexing in global storage
// let c = &mut Counter[addr];   // same as borrow_global_mut<Counter>(addr)
// let val = Counter[addr].value;

22.4 Macro functions (Sui Move 2024)

Macros are expanded at compile time and accept lambdas:

Standard macros you should know:

10u64.do!(|i|...), std::u64::range_do!(2, 8, |i|...) v.do_ref!(|x|...), v.do_mut!(|x| *x = *x + 1), v.do!(|x| consume(x))

v.map_ref!(|x| *x * 2), v.filter!(|x| *x > 3), v.fold!(0, |acc, x| acc + x)

v.any!(|x| *x == 0), v.all!(|x| *x < 10) vector::tabulate!(5, |i| i * i)

opt.do!(|x| ...), opt.destroy_or!(default), opt.map!(|x| x + 1)

assert!(cond, code), assert_eq!(a, b) (tests, std::unit_test)

module util::math;

public macro fun repeat($n: u64, $f: |u64|) {
    let n = $n;
    let mut i = 0;
    while (i < n) { $f(i); i = i + 1; }
}

// usage
fun demo(): u64 {
    let mut total = 0;
    repeat!(5, |i| total = total + i);   // 0+1+2+3+4 = 10
    total
}

22.5 Aptos lambdas and function values

// Aptos Move: inline + lambda (all Move 2 versions)
use std::vector;
let total = vector::fold(v, 0, |acc, x| acc + x);
vector::for_each_ref(&v, |x| { /* ... */ });

// Aptos Move 2.2+: function values
let add_one: |u64| u64 has copy + drop = |x| x + 1;
let y = add_one(41);

22.6 Feature matrix

Aptos Move 2.x Method/receiver syntax Yes (any first param of own type) Yes (self parameter) Vector index v[i] Resource index T[addr] Enums + match for (i in a..b) No (use macros) Compound assignment += Labeled blocks with return 'a v Macros (macro fun) No (inline functions instead) Only as macro arguments Yes (2.2 function values) let mut required public struct required