O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The tuple pattern

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? ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required