Tuples
If you want a collection of values with differing types, you’ll need a tuple. Tuples look pretty much just like Lists
, except they use parentheses instead of square brackets.
> (1, "one")
(1,"one") : ( number, String )
> ('a', 42, ["Life", "the Universe", "Everything"])
('a',42,["Life","the Universe","Everything"]) : ( Char, number, List String )
Just like with List
s, there isn't any generic tuple type in Elm. There is a potentially infinite number of tuple types, as a tuple's type depends on its member values. The type of two tuples will only match if all their contents also have matching types.
> (1, 2) == (3, 4)
False : Bool
> (1, 2) == ("three", "four")
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm
The right argument of (==) is causing a type mismatch.
5│ (1, 2) == ("three", "four")
^^^^^^^^^^^^^^^^^
(==) is expecting the right argument to be a:
( number, number' )
But the right argument is:
( String, String )
But unlike Lists, the number of members in a tuple is a part of its type, too.
> [5, 6] == [7, 8, 9]
False : Bool
> (5, 6) == (7, 8, 9)
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm
The right argument of (==) is causing a type mismatch.
5│ (7, 8, 9) == (10, 11, 12, 13, 14)
^^^^^^^^^^^^^^^^^^^^
(==) is expecting the right argument to be a:
( number, number' )
But the right argument is:
( number, number', number'' )
Repeating type variables
Here we see some new type variables--number'
and number''
.
In short, those '
s are a message that this number
may or may not be of the same type as the other number
s. It prevents the problems we'd have with a type signature like (number, number, number)
.
A tuple with three number
s could only mean either of these:
(Int, Int, Int)
(Bool, Bool, Bool)
On the other hand, a tuple (number, number', number'')
can act as either of those, as well as:
(Int, Bool, Bool)
(Int, Int, Bool)
(Bool, Bool, Int)
(Bool, Int, Int)
...and any other tuple with three number
values!
Something similar happens with wildcard type variable a
. You can see it if you make a tuple of empty Lists
.
> ([], [], [])
([],[],[]) : ( List a, List b, List c )