[swift-users] pattern matching in if improvement?

Maury Markowitz maury.markowitz at gmail.com
Sun Mar 27 08:50:11 CDT 2016

> On Mar 26, 2016, at 7:13 PM, Erica Sadun <erica at ericasadun.com> wrote:
> if 200...299 ~= statusCode { print("within 200-299") }

Yes, I should have touched on this one too.

My concern here is one of the concerns of the case version, it's cart before the horse. General programming practice for tests is something like:

(the thing you want to test) (the test) (what you test against)

This syntax reverses that ordering. Sure, this is a purely stylistic concern, but so much in programming is.

As someone else noted, we could simply allow this to be reversed, but that raises the question of why bother?

We already have the "in" operator. Everyone knows it, because you have to, especially under Swift 3. Using it in the if statement works exactly the same way it does in the for. And it requires nothing new to learn, unlike ~= which is an edge use-case.

More information about the swift-users mailing list