Pattern matching is a staple of functional languages. At its core, Swift is primarily an object-oriented language, like Objective-C is. However, there are many advantages to the way more functional style languages like Haskell and Erlang do things that the designers of Swift decided to include. Pattern matching in particular, saves us having to type much longer, and less readable statements to do the same checks.
I mean, which is easier to read:
case (_, 0, 0):
or
if (someTuple.1 == 0) && (someTuple.2 == 0)
They both will find the same thing, but one can be used in a switch statement, and the other has to dig into the internals of a tuple and write a much longer looking comparison to 0 for each. Not to mention the logical && there, to make sure both of them are true.
[Read more…]