[swift-users] [swift-evolution] for-else syntax

Howard Lovatt howard.lovatt at gmail.com
Sun Feb 5 16:27:25 CST 2017


It is a functional style, so typically you use return for continue and
throws to accomplish break and return, e.g.:

    // Continue
    let a = [0, 1]
    a.forEach { _ in return } // for _ in a { continue }

    // Break and return
    enum LoopControls<T>: Error {
        case exitLoop // Break
        case returnValue(T) // Return
    }
    do {
        do {
            try a.forEach { _ in throw LoopControls<Void>.exitLoop } // for
_ in a { break }
        } catch LoopControls<Void>.exitLoop {
            // Process break if you need to
        }
        try a.forEach { _ in throw LoopControls<Double>.returnValue(42.0) }
// for _ in a { return 42.0 }
    } catch LoopControls<Double>.returnValue(let value) {
        return value
    }



On Sun, 5 Feb 2017 at 2:32 pm, Rob Mayoff via swift-users <
swift-users at swift.org> wrote:

> On Fri, Feb 3, 2017 at 7:15 PM, Howard Lovatt via swift-users <
> swift-users at swift.org> wrote:
>
> Why not add to the library:
>
> extension Sequence {
>     func forEach(_ eacher: (Iterator.Element) throws -> Void, elser: ()
> throws -> Void) rethrows {
>
>
> This doesn't work with break or continue and changes the meaning of return.
>
> It would be nice for Swift to support Tcl-like exceptional return codes so
> we can build control flow functions that work with break, continue, and
> return properly.
>
> http://www.tcl.tk/man/tcl/TclCmd/return.htm
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-- 
-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170205/02286f78/attachment.html>


More information about the swift-users mailing list