A tuple is an immutable grouping of unnamed but ordered values. Tuples can hold any (or multiple) data type(s) -- including other tuples.
Tuples are defined as comma-separated values between (
and )
characters: (<element_1>, ... , <element_n>)
.
("one", 2) // Tuple pair (2 values)
("one", 2, true) // Tuple triplet (3 values)
Only tuples with the same length and the same types (in the same order) can be compared. Tuples have structural equality, which means they are equal only if all their values are equal.
(1, 2) = (1, 2) // Same length, same types, same values, same order
// => true
(1, 2) = (2, 1) // Same length, same types, same values, different order
// => false
(1, 2) = (1, "2") // Same length, different types
// compiler error
(1, 2) = (1, 2, 3) // Different length
// compiler error
There are three ways in which you can extract values from a tuple:
fst
and snd
functionslet person = ("Jordan", 170)
// Option 1: fst/snd
let name1 = fst person
let length1 = snd person
// Option 2: deconstruction
let (name2, length2) = person
// Option 3: pattern matching
match person with
| name3, length3 -> printf "%s: %d" name3 length3
Technically, you can access a tuples value using its .Item1
, .Item2
, ... properties, but this is discouraged and results in a compiler warning.
Some key things to know about tuples are: