[swift-evolution] [Pitch] Reimagining guard case/if case

Anton Zhilin antonyzhilin at gmail.com
Tue Oct 25 14:52:13 CDT 2016


Haravikk, I fully agree with you; before I read your post I thought I’d
write exactly that.
if <expression> matches <pattern>

1.
Another “ideological” reason:
= is just an operator, so lhs = rhs should be an expression, but meaning of
= is overloaded in this context. On the other hand, when you see a keyword,
you can’t mistake if lhs matches rhs for an expression. Analogy with for-in
plays here.

2.
Does this change simplify the syntax enough that we can remove optional
binding?
Is if x matches y? short/simple enough by comparison to if let x = y?
I think yes, because matches is so much clearer.

3.
We can do the same change for for case pattern in, plus eliminate where
clause? How about this:
for x in y, x matches z?, condition, w matches x

let results: [Either<Double, String>]
for item in results, item matches .left(let num), num >= 0 {
    print(num)
}

Looks like it’s closer to logical sequence of actions, than what we
currently have. Now we have “select .left elements, while iterating through
results, and only select those which are >= 0“.

P.S. We can leave where, or even if in this form of for-in.
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161025/d75a8814/attachment.html>


More information about the swift-evolution mailing list