The Tuple data type was added in ZPE 1.12.9 (Piper Pine Marten, September 2024). In essence, a tuple is much like a list, except that each value of a tuple is typed. Whilst ZPE introduced array data type some time before the tuple, the array data enforces that all values are of the same type. The tuple data type on the otherhand allows data types to be specified per element. It also has a slightly larger memory overhead.
Tuples are convenient to write and easy to implement. They use the simple bracketed syntax as used in mathematics and logic to represent tuples:
(10, "Hello world")
However, before the previous code could run, we need to have a few other things before it. Unlike lists, for example, they are built in two stages. These stages are:
- Definition
- Instantiation
The definition stage of a tuple involves declaring the tuple data types:
$x = (number, string)
After definition, using the exact same variable name as before, the tuple is then instantiated:
$x = (10, "Hello world")
Using tuples
A tuple can be used in the same way as any indexable element, using the subscript syntax:
$x = (number, string) $x = (10, "Hello world") // Should print "Hello world" print($x[1])