# Tuple
Tuple is an ordered collection of elements. Elements can be of any type. The tuple can contain from 2 to 22 elements.
The tuple is used similarly to the structure containing fields _1
, _2
, etc., where the number of fields is equal to the number of elements of the tuple.
For weight restrictions, see the Data Weight article.
# Examples
let x = (42, "waves", true)
x._2
Result: "waves"
let (a, b, c) = x
c
Result: true
# Tuple As Function Argument
func foo(a: (Boolean, String)) = {
a._1.toString() + "_" + a._2
}
foo((true,"waves"))
Result: "true_waves"
# Tuple inside match ... case
You can use match ... case
to match elements of a tuple with data types or with specific values.
Examples:
match (42, "waves") {
case (Int, String) => true
case _ => false
}
Result: true
match (42, "waves") {
case (x: Int, _) => x == 42
case _ => false
}
Result: true
match (42, "waves") {
case (0, y: String) => throw("Something went wrong")
case (42, y: String) => y
case _ => false
}
Result: "waves"