[swift-evolution] repeat loop enhancement

Tyler Cloutier cloutiertyler at aol.com
Wed Dec 9 14:58:17 CST 2015


I’m still of the belief that all types of loops could be condensed down to a single type of loop, the repeat-for loop

Consider:


The for-in loop:

repeat for x in 0..<5 {
    // code
}

The C style for loop and the while loop:

repeat for var i = 0 while someCondition(i) {
    // code
} 


The repeat while:

repeat for var i = 0 {
    // code
} while someConditionEvaluatedAfterFirstLoop() // The i variable would likely have to be considered in scope in the while expression


The currently discussed repeat loop

repeat 5 {
    // code
}

All follow a very basic pattern: The repeat keyword followed by a for declaration of loop scoped variables, then either a condition in the case of while or an iterator in the case of for-in. Optionally the language could include an “always” keyword that would specify a set of statements that were guaranteed to execute after each run of the loop. For example:

repeat for var i = 0 while i < 5 {
    
} always i += 1

If this were the case then all current loop constructs and their features could be subsumed by a single type of loop (including the to-be-removed C style for). This does have the problem of increasing the length of some current loop constructs though, specifically for-in.

Tyler


> On Dec 8, 2015, at 10:05 PM, André Videla via swift-evolution <swift-evolution at swift.org> wrote:
> 
> How about having a default implementation in the standard library for it? 
> We avoid syntactic sugar and we allow for this very simple yet useful expression.
> And we’ve seen solutions in this thread using autoclosurses and extensions
> 
> 5.times {
>> }
> 
> or a function
> 
> repeat(4) {
>> }
> 
> all sound good to me.
> 
> - André
> 
> 
>> On 09 Dec 2015, at 06:58, Chris Lattner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)
>> 
>> This is a very valid use case.
>> 
>> FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations.  There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).
>> 
>> I’d say that the pro’s and con’s of this are:
>> 
>> + Makes a simple case very simple, particularly important in teaching.
>> + Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
>> - It is “just syntactic sugar”, which makes the language more complex.
>> - It is a very narrow feature that is useful in few practical situations.
>> 
>> -Chris
>> 
>> _______________________________________________
>> 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/20151209/ece753c8/attachment.html>


More information about the swift-evolution mailing list