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 expression pattern

An expression pattern represents the value of an expression. Expression patterns appear only in a switch statement's case labels. The expression represented by the expression pattern is compared with the value of an input expression using the ~= operator.

The matching succeeds if the ~= operator returns true. By default, the ~= operator compares two values of the same type using the == operator. The following example presents an example of the expression pattern:

let position = (3, 5) switch position { case (0, 0):     print("(0, 0) is at the origin.") case (-4...4, -6...6):     print("(\(position.0), \(position.1)) is near the origin.") default:     print("The position is:(\(position.0), \(position.1)).") } 

We can overload ...

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