[T1, T2, ..., T255] is a dynamic container that stores from 0 to 255 elements in a single stack slot. Tuples cannot be serialized to cells and are only present on the stack during the compute phase. Yet they may be returned from get methods because contract getters operate directly on the stack.
Tolk supports two tuple types:
tuple— an opaque dynamic tuple with unknown shape;[T1, T2, ...]— a typed tuple with a known shape.
In many general-purpose languages, syntax
[A, B, C, ...] is used for array or list literals, while syntax (A, B, C, ...) is used for tuples. However, in TON, there are no arrays or lists. Tuple literals use the [A, B, C, ...] syntax, while (A, B, C, ...) is used by tensors: a distinct type that represents ordered collections of values that occupy multiple TVM stack entries.For example, a tuple [int, int, int] is a single stack entry containing three integers. In contrast, a tensor (int, int, int) signifies three separate integers that occupy individual stack entries or are serialized sequentially.Dynamic tuples
Atuple stores from 0 to 255 elements in one stack slot:
Typed tuples
Use[T1, T2, ...] to describe a tuple with a known shape, with non-primitive values not permitted:
Component access
Theget() method accesses a value by an index. When calling it on opaque dynamic tuples, explicitly provide a type of the retrieved value or rely on the compiler’s type inference:
tuple.{i} is permitted when the type is evident:
set() method writes a new value at an index. It does not create new elements — instead, it overrides existing ones.
get() or set(), an exception is thrown:
Accessing a tuple value takes a single TVM instruction, whereas accessing a tensor value requires stack shuffling.
last(), pop(), and others are available in the standard library.
Allowed values
Consider the following structure:t.push(somePoint) where somePoint is an instance of that structure raises an error — values that take more than one stack slot cannot be placed in a tuple. That is, only primitive, atomic types can be set:
Conversion to and from composites
For composite types, there are generic built-in methodsT.toTuple() and T.fromTuple() which convert composites to and from tuples. Length of the resulting tuple is equal to the number of stack slots occupies by a value of the converted type: if it occupies N stack slots, the resulting tuple has size N.
Lisp-style lists
Lisp-style lists in Tolk are nested two-element tuples. For instance,[1, [2, [3, null]]] represents the list [1, 2, 3]. An empty list is conventionally represented as null. From the type system perspective, a Lisp-style list is a tuple? with dynamic contents.
Process such lists using the following standard library module: