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

Haravikk swift-evolution at haravikk.me
Sat Jun 11 07:01:56 CDT 2016


> On 10 Jun 2016, at 21:15, Thorsten Seitz via swift-evolution <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.

This was why I mentioned parenthesis earlier, this example could be formatted more easily if the where clause moved into the case like so:

	if case .some(let Point.cartesian(x, y) where x < y) = json[“rect”]?[“origin”].flatMap(.init(rawValue:)) { … }

Though it’s probably a good candidate for spreading onto multiple lines in the first place, but I think that moving the where clause into the parenthesis clarifies its usage a bit if we have to move the where clause.

> 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`.

Still, I’m inclined to agree with this. I find the where clause coming last pretty logical as I interpret it as reading like “complete this pattern match only if this condition is true”, or in the for loop “do the following only if this condition is true”, the latter could be clarified if we could write it as for x in xs where x > 0 do { … }, though that’s how I tend to read blocks to begin with (I’d just like to be able to use the do keyword to make it explicit).

The other thing I like about keeping the where clause at the end is that it keeps the fine detail to last, allowing the more significant action/intent to be described first. It’s tricky, there are merits to both options, but I’d say that trailing where is more consistent with the new trailing where on functions; this similarly lets you declare the most significant piece first, then fill in the details later.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160611/f3fd4664/attachment.html>


More information about the swift-evolution mailing list