Expression Pattern Operator in Swift

Subscribe to my newsletter and never miss my upcoming articles

Hello people, Leo here.

Today we're explore one kind of hidden operator called pattern matching operator or expression pattern operator. This is behind the scene used as switch statement operator by Swift. So let's see how can we use it directly!

The image is because Van Gogh was an expressionist artist. Got it? ;)

The problem

You have some credit card number that must be inside a range.

So we can start with range operators. There's some ways you can write that. Let's start with the basics:

let creditCardNumber = "123456789012"

let rangeTest = 0...12

rangeTest.contains(creditCardNumber.count) // true

But let's explore a little more about Swift operators before we enter improve this code above.

One Sided Ranges

In Swift we can also use the range operator to do ranges that continue as far as possible in one direction. So we can rewrite the code above like this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 13

let range = ...creditCardMinSize // 1 

range.contains(creditCardNumber.count) // true

range.contains(-23) // true as well

On the (1) mark we can see the one sided range syntax. It means that "I want to create a range from most negative number to the creditCardMinSize this case is 13". So when we ask if it contains the -23 integer it will return true.

You can do this in the other direction too. From the number we are starting to the max positive number, so we can write things like this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 13

let range = creditCardMinSize...

range.contains(creditCardNumber.count) // false
range.contains(2000) // true

And finally we don't need the range variable, writing finally this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12

(creditCardMinSize...).contains(creditCardNumber.count) // true
(creditCardMinSize...).contains(2000) // true

But this isn't very accurate because if the credit card number is like "13241341341341344" it will be true too. So would be better if we close the range. Let's explore the pattern matching operator in the next section to do that.

Pattern Matching Operator

The pattern matching operator is used behind the scenes by the switch statement. To solve our credit card problem we can use exactly that to do what we want. The ~= operator is used to accomplish that, we can compare ranges without having the call any methods like contains or do any switch statement. The code below is an example of how to use it:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12
let creditCardMaxSize = 14

if creditCardMinSize...creditCardMaxSize ~= creditCardNumber.count { // 1
    print("Yep, is a credit card")
} else {
    print("Nope, not a credit card here")

The (1) shows how: to create a range with the closed range operator, and use the pattern matching operator ~= to compare if the credit card number is within the range. Now we don't need to make our logic complicated or use any methods or more variables to make range comparisons.

One last thing the code above is analogous to the code with the switch statement, example:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12
let creditCardMaxSize = 14

switch creditCardNumber.count {
    case creditCardMinSize...creditCardMaxSize:
        print("Yep, is a credit card")
        print("Nope, not a credit card")

That's it. Today we explore open and closed ranged operators, and the pattern matching operator solving the problem of check if its a valid credit card. Hope you have enjoyed the content as I enjoyed writing it.

Thanks for the reading and... That's all folks.

credit: image

No Comments Yet