Accessing
If we want to get the first and second names of a pair, we can use destructuring. Destructuring a tuple allows us to give names to the elements inside the tuple:
That single definition actually introduces in the current scope two
constants, alice
and bob
. Alternatively, if we still want to give
a meaningful name to a useless component, we can use a silent variable
for it, by prefixing it with _
:
Note how we renamed alice
as alice2
in order to avoid a collision
(redefinition) with previous one in the same top-level scope.
We can destructure nested tuples in the same manner:
This works well if we want to give a name to a component (like
greeting
above), but we might simply want the value of a component
without naming it. In that case, we use the binary operator []
:
The first component has index 0
, the second 1
etc.