[swift-evolution] Change `repeat` to loop indefinitely if no while clause is present
Dany St-Amant
dsa.mls at icloud.com
Wed May 11 05:26:59 CDT 2016
> On May 10, 2016, at 9:11 PM, Tyler Cloutier via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On May 10, 2016, at 5:56 PM, Chris Lattner <clattner at apple.com> wrote:
>>
>>
>>> On May 10, 2016, at 4:13 PM, Cole Campbell via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> I agree that repeat { } is ambiguous because you have to look to the end for a while clause to determine if it's infinite or not.
>>
>> Right, this is the downside that I see with “repeat {}”.
>
>
> Not to beat a dead horse, but isn’t this also true of
>
> repeat {
>
> } while true
>
> and
>
> while true {
> ...
> ...
> if condition {
> break
> }
> }
In all the code I ever seen, infinite loop are seldom really forever, they must exit one day mainly for graceful exit of the application. So break statement are to be expected in all infinite loop. The question of the readability of a infinite loop is not about whether or not the loop can exit, but about the intent or goal of the loop. Here the 'while true' strongly advertise up front that the loop is to run as long as it can; while for the 'repeat', one must go to the end to get that intent. So for readable code, a forever loop should preferably be written as:
while true { /* multi-line work */ }
And never as:
repeat { /* multi-line work */ } while true
Removing the trailing 'while true' of the 'repeat' does not help with reading and quickly understanding the code, for that to occurs a keyword will need to be added to the repeat itself; like the 'repeat forever' which is longer than the plain good old 'while true' at the top.
Dany
>>
>>> while true { } is preferable in that regard, but a compromise that I saw mentioned is:
>>>
>>> repeat forever { }
>>
>> This would require taking “forever” as a keyword if we supported “repeat N {", something we wouldn’t want to do.
>>
>> Another option is to make it a statement modifier, which wouldn’t require taking it as a keyword (but also doesn’t read as well):
>>
>> forever repeat { }
>>
>>
>> Personally, I don’t see this as a big enough improvement over “while true” to be worth introducing complexity for.
>
> If you are referring to “forever", I also don’t think that adding a new keyword is an improvement over “while true”.
>
>> -Chris
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list