[swift-evolution] [Pitch] Retiring `where` from for-in loops
Dany St-Amant
dsa.mls at icloud.com
Fri Jun 10 19:29:37 CDT 2016
Le 10 juin 2016 à 13:59, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> a écrit :
>> But most importantly (and this is really the kicker for me) there are times when the "where" syntax provides the maximum amount of clarity in the context of my code, and I don't want to lose that expressive power.
>
> This is the key and salient point here. Would you be able to share some examples where the `where` syntax provides a clear win in clarity? That would definitely be a huge pro, if it can be used to solve issues in expressiveness much like `guard` allowed elimination of the pyramid of doom.
In this case, I think it is hard to come with perfect example as beauty is in the eye of the beholder. Having the flexibility of using guard else { continue }, if not { continue } or where, is at some level a bit like choosing between:
if !(dataA == value || dataB == value) { }
if dataA != value && dataB != value { }
if dataA != value { if dataB != value { } }
All three are valid, and the baby pyramid of doom version may even be the right option depending on how you think the code will evolve.
Back to the where vs guard/if not continue, I see the former as "I want/like" and the laters as "I don't want/like".
for book in books where book.language == .French
{
guard book.readCount == 0 else { continue }
if book.originalLanguage == .English { continue } // should read the original
// My apologies to translators who might feel offended.
}
One could put all the conditions as a single where, as a multiple guard, as multiple if, and as a single guard or if, but I think that my keyword choices better convey my message:
I want a book written in French
Protect me from what I have read
No translation from English for me
(Not that those are worst than others, but I can read the original... Apologies again to translator)
Dany
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160610/8f9b829e/attachment.html>
More information about the swift-evolution
mailing list