Skip to content
LogoLogo

Lists

list(T) is a dynamic list and list(T, n) a fixed-length one. Both work in storage and in memory.

lists.lithe
import "std/evm";
import "std/list";
 
error OutOfRange of uint;
 
storage is record {
  (* A dynamic list has a length, which `List.push` grows. *)
  items : list(uint);
  (* A fixed list has its length in its type. *)
  slots : list(address, 3);
}
 
external function append(const value : uint) is {
  List.push(storage.items, value);
}
 
external function drop() is {
  List.pop(storage.items);
}
 
external view function get(const i : uint) : uint
  raises OutOfRange
is {
  assert(i < storage.items.length, OutOfRange(i));
} with storage.items[i]
 
external view function length() : uint is
  storage.items.length
 
external function setSlot(const i : uint) is {
  storage.slots[i] := Evm.sender();
}
 
(* List literals build lists in memory. *)
external pure function second() : uint is {
  const primes : list(uint, 4) := [2, 3, 5, 7];
} with primes[1]
 
external pure function count() : uint is {
  const names : list(string) := ["a", "b", "c"];
} with names.length
  • List.push appends. An index past the end, read or written, reverts with Panic(0x32).
  • List.pop drops the last element. An empty list reverts with Panic(0x31).
  • get checks the bound itself, to raise a typed error instead of a panic.
  • Fixed lists can be ABI parameters (address[3]). Dynamic lists cannot yet.