[swift-evolution] repeat loop enhancement

Jason Pollack jlpollack at gmail.com
Wed Dec 9 08:41:21 CST 2015


Solutions like this look reasonable, until you try to use normal control
flow statements as you would in other loops. In the closure, a break or
continue are illegal, and a return acts like a continue.

var ok = true

5.times {
    ok = someTest()

    if (!ok) {
        //break - Nope!
        return //Sure, but loop continues anyway
    }
}

Sure, it's a contrived example, but it's easy to see how this construct is
not like other loops.

On the other hand, it might be nice to be able to decorate a closure such
that break and continue behave as one might expect inside a loop, but that
sounds like a can of worms.




On Wed, Dec 9, 2015 at 1:05 AM, André Videla <zephyz at me.com> 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> wrote:
>
> On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <
> 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
> 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/117495d4/attachment.html>


More information about the swift-evolution mailing list