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

Xiaodi Wu xiaodi.wu at gmail.com
Fri Jun 10 23:35:27 CDT 2016


On Fri, Jun 10, 2016 at 10:49 PM, Karl via swift-evolution <
swift-evolution at swift.org> wrote:

> I’m sorry, but those statistics are not useful. The Swift standard library
> is not representative of real-world Swift code, or what Swift developers
> expect or appreciate about the language. You also need to consider that
> lots of people are reluctant to support new syntax for fear that it might
> as soon disappear with the next drop (*uhum*…).
>

More likely, lots of people don't know that this syntax exists. Earlier
today we heard that one experienced Swift user didn't learn about the
feature until joining this list!

Earlier, Jonathan Hull raised the example of stdlib `.forEach()`. Let's
compare discoverability of these two features. For a method in the stdlib,
I merely type `.` after the receiver in any IDE worth its salt, and I can
find `.forEach()`. Or, say I'm reading someone else's code. I see
`.forEach()` and I click on it, and in any IDE worth its salt, I see all
the info I need in a panel to the side. Now let's think about the
discoverability of the `where` clause. I type `for foo in bar`--what
tooling could I possibly use that would remind me about the `where` clause?
Or let's say that I encounter the rare `for foo in bar where baz`, where do
I click in Xcode for info on what `where` does? My best bet is to google
"Swift where"!

Of course, you might say, this is an argument for better tooling and better
documentation. Sure. But it's also indicative of a fundamental distinction
between library method names and language keywords. In essence: if it's a
method, you're expected to look it up; if it's a keyword, you're assumed to
know it. This has been the case for every language I've encountered. So one
question [not the only question] to ask here is: is the `where` clause so
intuitive, or does it provide such utility, that we can rightly have that
expectation of those who use loops (which, I'm going to make a wild guess,
is the set of all users)?

Gathering useful statistics about the popularity of features in the Swift
> language is more involved than simply grep-ing the standard library.
>
> I know for a fact that it’s not representative of the code I’ve seen, or
> that I see people write, and it’s not reflective of that small amount of
> joy they feel when they realise basic, mundane stuff like this just works
> and stays readable. They’ve lived with C-loops and continues, Python list
> contractions and the like, and this is just better.
>

I rather like Python list comprehensions--what's so terrible about them
that they're merely to be "lived with"? If we take inspiration from Python,
we might want to consider replacing `where` with `if` like you suggest
below.


> Somebody mentioned LINQ - that’s another sad story; It’s hard to reason
> about the performance characteristics of your code, and tiny,
> non-functional changes can have drastic impacts on performance. Making
> collections of data easy to filter is something a 21st-century programming
> language needs to do, and we could get things a whole lot “wronger” than a
> simple boolean predicate attached to “for" loops.
>
>
> There is something (which may have gotten lost in my mail): the “for _ in
>  _” loop is a data-driven loop. It’s unique compared to other loops such as
> “while” or “repeat/while”, which simply check a Bool condition. You don’t
> even have a loop counter, because your place in the list doesn’t matter -
> it’s about iterating over a collection of individual things. With that
> context, having the ability to filter inline is sort-of imperative.
>

Not that this way of thinking is determinative, but of languages in the C
family with `for...in` loops (including C++'s range-for loop, etc.), how
many have the ability to filter inline by the use of a keyword? I don't
know of any. Not even Python, which has a filtering syntax for its list
comprehensions. Many have a method such as .filter() [Rust] or .Where()
[C#], as does Swift. This certainly doesn't mean that Swift *shouldn't*
have a keyword, but it does suggest that it's not really "imperative."


> I think its existence is entirely justified.
>
> I would be more interested in discussing how exactly we could make it
> clearer than if it should exist at all. Personally, I have no attachment to
> the “where” keyword (I mean, I got it wrong practically every time in my
> mail, calling it “while”). But I’ve thought about alternatives and renaming
> (possible replace it with just “if”? as in “for number in 0..<10 if number%
> 2 == 0 { print(“\(number) is an even number") }” ). I’m not sure I can
> think of something that’s clearly much better than what we have now.
>
> Karl
>
> On 10 Jun 2016, at 17:24, Erica Sadun via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
> On Jun 10, 2016, at 9:22 AM, Erica Sadun via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
> On Jun 10, 2016, at 8:02 AM, Xiaodi Wu via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> On Fri, Jun 10, 2016 at 7:18 AM, Haravikk <swift-evolution at haravikk.me>
> wrote:
>
>> * The word "where" does not consistently imply `break` or `continue`. In
>> current Swift, `where` implies `break` in the context of a `while` loop and
>> `continue` in the context of a `for` loop. Some users intuitively guess the
>> correct meaning in each context, while others guess the wrong meaning.
>> Therefore, the only way to learn for sure what `where` means in any context
>> is to read the rulebook. That, by definition, means that this is
>> unintuitive.
>>
>>
>> This is an argument for renaming the where keyword on for loops to be
>> more clear, or to somehow integrate continue/break to be more explicit
>> about what the developer intends for it to do.
>>
>
> Sure: I conclude that the keyword should be *either* removed *or*
> reformed; both outcomes could address the issue.
>
>
> This is my stance as well and I reserve the right to flit between both
> choices until we've fully talked it through.
>
> One more data point.
>
> In the standard library there are just under 950 uses of "for in loops".
> There are 3 uses of "for in while" :
>
> private/StdlibUnittest/StdlibUnittest.swift.gyb:    for j in
> instances.indices where i != j {
> public/core/Algorithm.swift:  for value in rest where value < minValue {
> public/core/Algorithm.swift:  for value in rest where value >= maxValue {
>
> -- Erica
>
>
> Actually a slight correction to that. I forgot to add the space after "in"
> on the first search. It's just over 600 and 3. Among the 600 are a few
> false positives but not many.
>
> -- E
>
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160610/5923123f/attachment.html>


More information about the swift-evolution mailing list