[swift-evolution] for-else syntax

Kevin Nattinger swift at nattinger.net
Wed Feb 1 12:17:53 CST 2017


I ran into this not too long ago. Something to handle this case would definitely be nice, but I agree we shouldn’t take python’s syntax with different semantics.

> On Feb 1, 2017, at 9:30 AM, Dimitri Racordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I agree. A for-else loop with different semantics than python would be error-prone for many people.
> 
> 
>> On 1 Feb 2017, at 18:17, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> One possible con: this is subtly but extremely different from Python, where a for loop's else clause is executed only if there was no `break` from the loop.
>> On Wed, Feb 1, 2017 at 8:48 AM Chris Davis via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Hi,
>> 
>> Often when I’m programming I stumble upon this scenario:
>> 
>> I have a list of items that may or may not be empty - if it’s full, I do one thing, if it’s empty I do something else, my code looks like this:
>> 
>> class Example_1
>> {
>>     let names = ["Chris", "John", "Jordan"]
>>     
>>     /// Loop over names, if no names, print no names
>>     func run()
>>     {
>>         for name in names
>>         {
>>             print(name)
>>         }
>>         
>>         if names.count == 0
>>         {
>>             print("no names")
>>         }
>>     }
>> }
>> 
>> let exampleOne = Example_1()
>> exampleOne.run()
>> 
>> However, Personally, I would find it more pleasing to write something like this:
>> 
>> class Example_2_Proposed
>> {
>>     let names:[String] = []
>>     
>>     /// Loop over names, if no names, print no names
>>     func run()
>>     {
>>         for name in names
>>         {
>>             print(name)
>>         } else {
>>             print("no names")
>>         }
>>     }
>> }
>> 
>> let exampleTwo = Example_2_Proposed()
>> exampleTwo.run()
>> 
>> The difference here is a “for-else” type syntax where if there were no items in the array it would simply fall through to the else statement.
>> 
>> What would be the pros/cons of introducing such syntax?
>> 
>> Is there’s a way of doing something similar in swift already?
>> 
>> Thanks
>> 
>> Chris
>> 
>> 
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto: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/20170201/28cd937b/attachment.html>


More information about the swift-evolution mailing list