Skip to content
LogoLogo

Maps

map(K, V) maps keys of type K to values of type V. It lives in storage, every key starts at zero, and keys cannot be enumerated.

maps.lithe
import "std/evm";
 
storage is record {
  balances : map(address, uint);
  (* Maps nest: owner -> spender -> amount. *)
  allowances : map(address, map(address, uint));
}
 
(* A missing key reads as the zero value. *)
external view function balanceOf(const who : address) : uint is
  storage.balances[who]
 
external function setBalance(const amount : uint) is {
  storage.balances[Evm.sender()] := amount;
}
 
(* Deleting writes the zero value back. *)
external function clear() is {
  delete storage.balances[Evm.sender()];
}
 
external function approve(const spender : address; const amount : uint) is {
  storage.allowances[Evm.sender()][spender] := amount;
}
 
external view function allowance(const owner : address; const spender : address) : uint is
  storage.allowances[owner][spender]
  • delete writes a value's zero, for any type: delete storage.orders[id]; empties a record's strings and lists too.
  • Map keys are single words (integers, bool, address, bytesN, and nominal types over them), strings, or bytes. Values can be words, maps, or records of words.