[swift-evolution] [Pitch] Retiring `where` from for-in loops

Xiaodi Wu xiaodi.wu at gmail.com
Fri Jun 10 19:48:23 CDT 2016


Yes, I rather agree with the implication here. By using "where", you are
saying that you want something, and it feels more fluid because you really
aren't concerned with the things you don't want. The argument, essentially,
is that "where" is letting you get away with too much unstated. Namely, it
is leaving implicit what you want the loop to do with the books you won't
read. Maybe you feel bad rejecting all those authors. But it is a cop-out
to let the loop do it for you, like King Henry VIII of England ordering one
of his ministers to sign the death warrant for his wife.

I argue that "where" clauses are a false economy of words, and that you
should indicate overtly (to the reader of your code even if it's clear to
yourself) how it is you wish for non-French books to be rejected, for
rejected they must be in some way. By contrast, guard says, reject those
unwanted books by continuing, or breaking, or a fatal error. I know, it's
unpleasant, but you are rejecting them by act or by omission; and of those
only the act shows intentionality.

On Fri, Jun 10, 2016 at 19:29 Dany St-Amant <dsa.mls at icloud.com> wrote:

>
> 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/20160611/ff88d223/attachment.html>


More information about the swift-evolution mailing list