[swift-evolution] Pitch: "while" clause on for-loops

Jacob Bandes-Storch jtbandes at gmail.com
Thu Dec 31 02:42:42 CST 2015


Currently, for-loops admit a "where" clause:

    for x in seq *where* cond {
        ...
    }

    behaves like

    for x in seq {
        if !cond { continue }
        ...
    }


I'd be interested in a "while" clause:

    for x in seq *while* cond {
        ...
    }

    would behave like

    for x in seq {
        if !cond { break }
        ...
    }


This is one area where C-style for-loops would have provided a clean
solution (combining multiple conditions with &&), but once they're removed
any extra conditions will have to move inside the loop.

Pros:
- It's a simple way to express a common piece of control flow that
otherwise requires negation and another set of braces.
- Its meaning is easy to understand.

Cons:
- It's a new feature.
- Naming might cause confusion with while-loops.

Open questions:
- How/could it be combined with "where" clauses? Would order matter?
- Does anyone else care?


Thanks,
Jacob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151231/717eb68f/attachment.html>


More information about the swift-evolution mailing list