Following Brent's logic that the for-in where should mimic the switch statement functionality, then this example:<br><br>for (eachKey, eachValue) <br>where eachValue > 5 <br>in theKeyValuePairs {... }<br><br>Should read like:<br><br>for (eachKey, eachValue where eachValue > 5) in theKeyValuePairs {... }<br><br>And,<br><br>for (eachKey, eachValue) <br>where eachKey % 2 == 0 && eachValue > 5<br>in theKeyValuePairs {... }<br><br>Should read like:<br><br>for (eachKey where eachKey % 2 == 0, eachValue where eachValue > 5) <br>in theKeyValuePairs {... }<br><br>Personally, I think the first examples are much more readable. But if we go with the proposal as it stands, we will be creating yet another syntax structure for where.<br><br>Just wanted to point that out.<br><br><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 10, 2016 at 11:06 AM Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> I am curious, is there any conflict with the reasoning to move where here compared to the accepted SE-0081 "Move where clause to end of declaration" <a href="https://lists.swift.org/pipermail/swift-evolution-announce/2016-May/000161.html" rel="noreferrer" target="_blank">https://lists.swift.org/pipermail/swift-evolution-announce/2016-May/000161.html</a><br>
><br>
> We moved the where clause to before the body in one case (SE-0081) and now we are trying to move the where clause from before the body to right next to the variable.<br>
><br>
> In SE-0081: "With the proposed change, where clauses do not impede the main declaration and are also more easily formattable"<br>
><br>
> I know these are different uses but it is beginning to hurt my head where all the where clauses are suppose to go in different contexts<br>
<br>
I understand why this would confuse you, but there are practical reasons for them to be different.<br>
<br>
The `where` clause is used in a couple different ways:<br>
<br>
* In a declaration, it specifies additional type information—for instance, that an associated type conforms to a certain protocol or matches another associated type on a different type parameter.<br>
<br>
func append<C: Collection>(contentsOf other: C) where C.Iterator.Element == Self.Iterator.Element { … }<br>
<br>
* In a `case` statement or other pattern, it specifies a Boolean condition which must be met for the pattern to match.<br>
<br>
switch value {<br>
case .result(let character) where emoji.contains(character):<br>
print("Hello, millennial!")<br>
case .result(_):<br>
print("Hello, fogey!")<br>
default:<br>
print("Goodbye, world!")<br>
}<br>
<br>
Both versions of the clause refine a match to specify additional conditions; that's why they use the same keyword. But they're actually quite different.<br>
<br>
In particular, one big difference between the two is that you can only attach one `where` clause to a declaration, but there might be many `where` clauses in a pattern match. For instance, in this example:<br>
<br>
enum X { case a, b }<br>
switch X.a {<br>
case .a, .b where 1 == 0:<br>
print("Matched")<br>
default:<br>
print("Didn't")<br>
}<br>
<br>
The `where` clause only applies to the `.b`, not the `.a`, so the code will print "Matched". To make it print "Didn't", you'd have to write `case .a where 1 == 0, .b where 1 == 0`. In other words, the `where` clause is part of the specific pattern, not the `case` statement as a whole, and so it *must* be attached to the specific pattern.<br>
<br>
The `for` syntax, however, does not respect this rule. It inserts the `in` clause between the pattern (the thing after the `for` keyword is a pattern too, even if it doesn't have a `case` keyword) and its `where` clause. That's not consistent with `switch`-`case`'s syntax, and in practice, I find it leads to confusion about what `where` does.<br>
<br>
For a declaration, however, there can only be one `where` clause, and you can equally well view it as applying to only the generic parameter list (the old way) or to the entire declaration (the new way). There are several reasons why it's very convenient to put it at the end:<br>
<br>
1. Declaration `where` clauses tend to involve lots of verbose names and be very long.<br>
2. Declaration `where` clauses sometimes need to be attached to declarations with no generic parameter list, like `extension` and hopefully soon `associatedtype`.<br>
3. Declaration `where` clauses embedded in a generic parameter list end up wedging a bunch of usually unimportant information between two of the most important parts of the declaration: the list of generic parameters and the list of regular parameters. This ends up turning the whole thing into a jumbled mess.<br>
<br>
So even though both kinds of `where` clause do broadly similar things, they face very different constraints and pressures, and thus are positioned a little bit differently. That's unfortunate, but probably unavoidable.<br>
<br>
The proposal being discussed in this thread is actually trying to improve this situation by making pattern `where` clauses in `for` loops match pattern `where` clauses in `switch` statements. In other words, I believe it actually improves consistency over the status quo. So if you're confused about where you put `where`, this should make it a little easier to keep things straight.<br>
<br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>