# [swift-evolution] [Proposal] Invert the order of pattern match operator

David Rodrigues david.ndh at gmail.com
Thu Apr 7 06:57:53 CDT 2016

```Hi all,

Swift has a pattern match operator, ~=, which is unknown to many (like me
until a few weeks ago), that performs a match between a value and a certain
pattern, e.g. checking if an integer value is contained in a range of
integers.

This operator may be little known, but it plays a key role in the language
since it's used behind the scenes to support expression patterns in
`switch` statement case labels, which we all know are extremely popular.

let point = (2, 4)
switch point {
case (0, 0):
print("The point is at the origin")
case (0...4, 0...4):
print("The point is in the subregion")
default:
break
}

Most of the time we don't use the operator directly but it is available and
can be handy in certain conditions.

let point = (2, 4)
switch point {
case (let x, let y) where 0...4 ~= x && 0...4 ~= y:
print("The point is in the subregion")
default:
break
}

However the current syntax is not ideal (in my opinion). We're not really
declaring the operation that we want to do, and that has an impact in the
expressivity and readability of the code. Currently we're doing matches
like "if blue is the ocean" instead of "if the ocean is blue" or "if the
ocean contains the whale" instead of "if the whale is in the ocean".

For that reason, I would like to suggest inverting the order of the
operator to match more closely our logical thought.

case (let x, let y) where x =~ 0...4 && y =~ 0...4: // Proposed
// vs
case (let x, let y) where 0...4 ~= x && 0...4 ~= y: // Current

I have an ongoing proposal to suggest this change and it contains a little
more context. It is available here:

https://github.com/dmcrodrigues/swift-evolution/blob/proposal/invert-order-of-pattern-match-operator/proposals/NNNN-invert-order-of-pattern-match-operator.md
.

Any feedback is very welcome.

Thank you.

David Rodrigues
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/84d236f1/attachment.html>
```