Skip to content
LogoLogo

Tuples

Tuples group values by position. Use them to return several values, and destructure them with const (a, b) : (T, U) := ... or var (a, b) : (T, U) := ....

tuples.lithe
import "std/evm";
import "std/uint";
 
storage is record {
  (* Tuples can be stored, and pack like a struct. *)
  pair : (uint32, address);
}
 
pure function split(const amount : uint) : (uint, uint) is
  (amount / 2, amount % 2)
 
external function run(const amount : uint) is {
  (* Destructure a tuple into names. *)
  const (half, rest) : (uint, uint) := split(amount);
  storage.pair := (uint32(half + rest), Evm.sender());
}
 
(* Several return values are a tuple. *)
external view function info() : (uint32, address) is
  storage.pair
 
external pure function divRem(const a : uint; const b : uint) : (uint, uint) is
  Uint.divRem(a, b)
 
(* `var (a, b)` binds elements that `(a, b) := ...` can step together. *)
external pure function gcd(const a : uint; const b : uint) : uint is {
  var (x, y) : (uint, uint) := (a, b);
  for _ : uint in 0 .. 256 until y = 0 do {
    (x, y) := (y, x % y);
  }
} with x