[swift-evolution] [swift-evolution-announce] [Review] SE-0099: Restructuring Condition Clauses

Brandon Knope bknope at me.com
Tue May 31 15:43:20 CDT 2016


Why would that argue for the removal of where

if let y = y where y != 5 && x < z

I would still prefer:
if let y = y where y != 5, x < z, let z = someOptional where z == 10

To me, where still has a place. And that place is saying "hey this is a recently introduced or shadowed variable, so don't go looking very far for what is referring to". 

If I understand correctly, this would be rewritten in the proposed syntax as:

if let y = y; y != 5; x < z; let z = someOptional; z == 10 { }

Which technically could be written as:

if let y = y; x < z; let z = someOptional; z == 10; y != 5 { } // y != 5 arbitrarily moved to the end 

Now y != 5 is at the end and a quick glimpse of this line could be confusing:
1. Is this y a previous variable like x is?
2. Is it a shadowed variable?

This kind of information is lost while glancing the line (and people do glance especially with long conditional clauses). This makes having where relevant in my opinion. It aids in readability and expressiveness. 

Brandon 




> On May 31, 2016, at 4:25 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
>> On Tue, May 31, 2016 at 3:16 PM, Brandon Knope <bknope at me.com> wrote:
>> And why couldn't we propose that it should?
> 
> And that is this proposal; the options considered for the spelling are comma, semicolon, and `&&`.
> 
> Now we circle back to my earlier point. The introduction of any of these would argue for the elimination of `where`, for the reasons I've stated above. Briefly, everything that can be expressed using `where` could be expressed using the comma, semicolon, or `&&`. Since there is no good way to enforce the semantic relationship requirement for `where`, only a proxy rule about variable names can be contemplated, which as I've demonstrated is ineffective.
> 
> If you're asking why commas or semicolons have been proposed instead of `&&`, that argument has been made above by others.
> 
> 
>> Brandon 
>> 
>>> On May 31, 2016, at 4:14 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>>> 
>>>> On Tue, May 31, 2016 at 3:08 PM, Brandon Knope <bknope at me.com> wrote:
>>>> What is wrong with:
>>>> 
>>>> if let y = y && x < z
>>>> 
>>>> They are, after all, independent from each other.
>>> 
>>> That won't compile.
>>>  
>>> 
>>>> Brandon 
>>>> 
>>>>> On May 31, 2016, at 3:59 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>>>>> 
>>>>>> On Tue, May 31, 2016 at 2:51 PM, Christopher Kornher via swift-evolution <swift-evolution at swift.org> wrote:
>>>>> 
>>>>>> 
>>>>>>> On May 31, 2016, at 1:47 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> On Tue, May 31, 2016 at 2:45 PM, Christopher Kornher via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>>>>>> 
>>>>>>>>>> Not allowed:
>>>>>>>>>>>>>>>>>>>> let a = a
>>>>>>>>>> let b = b where b > 10 && a > 5
>>>>>>>>> 
>>>>>>>>> Why would this not be allowed by your rule? You're making use of `b` in your where clause. As I demonstrated above, essentially any assertion can be rewritten to work around your rule. In general:
>>>>>>>> 
>>>>>>>> It is not allowed because  ‘a’ is defined in the line above. It must be defined in the ‘if let’ associated with the where in which it is mentioned.
>>>>>>> 
>>>>>>> That's a much more restrictive where clause than you proposed earlier. You'd not be able to write:
>>>>>>> 
>>>>>>> ```
>>>>>>> let b = b where b > anyOtherVariable
>>>>>>> ```
>>>>>> 
>>>>>> 
>>>>>> The definition is not a formal one, but that was the intent.
>>>>>> 
>>>>>> ```
>>>>>> let b = b where b > anyOtherVariable
>>>>>> ```
>>>>>> is legal as long as `anyOtherVariable` is not defined within the entire condition clause
>>>>> 
>>>>> 
>>>>> You can propose that rule, but it doesn't solve the issue. If, today, I've got
>>>>> 
>>>>> ```
>>>>> let x = 1
>>>>> let y: Int? = 2
>>>>> let z = 3
>>>>> 
>>>>> if let y = y where x < z {
>>>>>   // do stuff
>>>>> }
>>>>> ```
>>>>> 
>>>>> your rule simply forces
>>>>> 
>>>>> ```
>>>>> if let y = y where y == y && x < z {
>>>>>   // do stuff
>>>>> }
>>>>> ```
>>>>> 
>>>>> The point is, the semantic relationship between what comes before and after `where` exists in the mind of the human reader only.
>>>>> 
>>>>> _______________________________________________
>>>>> 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/20160531/6c41c624/attachment.html>


More information about the swift-evolution mailing list