[swift-evolution] [DRAFT] Regularizing Where Grammar (was Re: Add a while clause to for loops)

Thorsten Seitz tseitz42 at icloud.com
Fri Jun 10 16:36:26 CDT 2016


> Am 10.06.2016 um 22:26 schrieb Xiaodi Wu <xiaodi.wu at gmail.com>:
> 
> 
> 
> On Fri, Jun 10, 2016 at 3:15 PM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> -1
> 
>> Am 10.06.2016 um 21:10 schrieb Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>> 
>> if case .some(let Point.cartesian(x, y)) where x < y = json["rect"]?["origin"].flatMap(.init(rawValue:)) { … }
> 
> This: `where x < y = json[…]` is *very* unreadable and probably unparseable as it is missing a delimiter between the boolean expression and the expression behind the assignment operator. And I do not count the assignment operator as a sufficient delimiter.
> 
> Agreed. Unless this readability issue can be solved, I'm concerned about this particular reshuffling.
>  
> 
> I’ll give the following counterargument against this shuffling around of `where` or trying to eliminate it:
> 
> 	if case .some(let x) = someExpression where x > 0
> 
> 	for x in xs where x > 0
> 
> These are actually very consistent in their grammar when you look at it like follows:
> 
> 	<target> <assignment op> <source> where <condition>
> 
> 
> 	if case .some(let x) = someExpression where x > 0
> 
> 	target: `case .some(let x)`
> 	assignment op: `=`
> 	source: `someExpression`
> 	condition: `x > 0`
> 
> 
> 	for x in xs where x > 0
> 
> 	target: `x`
> 	assignment op: `in`
> 	source: `xs`
> 	condition: `x > 0`
> 
> 
> That is I’m arguing the `where` grammar is already quite regular and should not be changed. And yes, I’d like to have `where` back in `case`. And `let`.
> 
> The inconsistency argument is that this grammar is inconsistent with usage when switching over cases, etc., not that it's inconsistent with usage in `if`.

For switching the whole <assignment op> <source> part is not needed because the <source> is the expression behind `switch`.
I don’t have a problem if that part vanishes from the middle of the grammar instead of from the end.


> 
> Principally, though, the problem is that `where` leaves implied but unanswered the question of "what happens where not?" For an `if` statement the answer is inherent to the word "if," but because it is inherent, it becomes redundant and can be replaced with a comma.

I do not agree with that replacement but I’m glad that at least we didn’t end up with semicolons (shudder)...


> Likewise `while`. For a `for` loop, `where` cannot be replaced with a comma because it is not implied by the word `for`; but because it is not redundant, it is also not inherent in the meaning of the word "where.“

I don’t agree. `for x in xs where condition` is crystal clear to me. It is just like a mathematical set with a condition reads:

{ x ∈ xs | x > 0 } reads as "x in xs where x greater than zero“

So why do you claim the meaning of `where` is unclear in a `for` loop?

-Thorsten

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160610/f6ea8858/attachment.html>


More information about the swift-evolution mailing list