April 2017
Intermediate to advanced
316 pages
9h 33m
English
A tuple pattern is a comma-separated list of zero or more patterns, enclosed in parentheses. Tuple patterns match values of corresponding tuple types.
We can constrain a tuple pattern to match certain kinds of tuple types using type annotations. For instance, the tuple pattern (x, y): (Double, Double) in the declaration, let (x, y): (Double, Double) = (3, 7), matches only tuple types in which both elements are of the Double type.
In the following example, we match the pattern by binding the name, checking whether age has a value, and finally, if the address is of the String type. We use only the name that we need and, for age and address, we use the wildcard pattern to ignore the values:
let name = "John" let age: Int? ...
Read now
Unlock full access