[swift-evolution] A (better) Swift Equivalent For The Classical For-Loop With Numeric Scalars

Greg Parker gparker at apple.com
Mon Mar 21 06:37:19 CDT 2016

> On Mar 19, 2016, at 12:46 AM, Dmitri Gribenko via swift-evolution <swift-evolution at swift.org> wrote:
> Hi Ted,
> Thank you for starting this thread.  I agree that removing the C-style
> for loop has degraded the readability and clarity of some of numerics
> code.
> In the feedback to SE-0007 many people have said that they can convert
> their code to for-in loops, but I think this actually means that in
> code that is typically written in Swift today, loops primarily operate
> on sequences and collections.  It means that numerics is a domain that
> not many people work in.  But it is a very important domain
> nevertheless, and clarity for numerics code matters at least as much
> as it does everywhere else.
> I think one way to approach this discussion would be to present
> multiple concrete code samples that contain C-style for loops and are
> awkward to write without them.  We can then try looking for patterns,
> generalize and simplify, and discuss possible solutions.

Let me emphasize this more strongly. *Concrete*, *real-world* examples are quite likely the only way that you are going to get `for(;;)` back or get any sort of semantically-similar syntactically-different replacement.

There have been lots of suggestions. None of them are perfect. If we assume that there is in fact no perfect solution then the only way to proceed is to provide sufficient justification for some imperfect solution.

I'm still waiting for something like this: "We ported our code to Swift 3. Here are 5 reasonable-looking for(;;) loop shapes from 150 call sites, and here are their ugly-looking rewrites."

(Personally I think removing for(;;) without direct replacement was too aggressive. That view lost. Now its advocates will need to do more work to upend the status quo.)

Greg Parker     gparker at apple.com     Runtime Wrangler

More information about the swift-evolution mailing list