0

The following code seems to work just fine in swift:

for (var i = 0, x = 0; i < 10; i++, x++) {
    println ("i is \(i) x is \(x)");
}

1) I can not find any documentation that says the comma operator is supported or not.

2) The swift-playground sometimes crashes when modifying a for loop to include a second counter (, x = 0).

I'm I wrong to expect the above code to work, or is this just a playground issue? Any reference to documentation on this would be appreciated.

Bill
  • 1,588
  • 12
  • 21

1 Answers1

1

It is supported and it seems to work fine for me in a Playground in Xcode 6.1 GM 2 (6A1046a). You can find the documentation for it in the Language Reference section of the Swift Programming Language guide.

Specifically (slightly tweaked to fit the SO answer format):

for-statement → for ­for-init ­; ­expression­­ ; ­expression­ ­code-block­
for-statement → for­ (­ for-init­ ; ­expression­ ; ­expression­ ­) ­code-block­

for-init → variable-declaration­ expression-list

expression → prefix-expression­ binary-expressions
expression-list → expression­ | expression­ , ­expression-list

Note the , in expression-list

Mike S
  • 41,895
  • 11
  • 89
  • 84
  • I looked right past it. There might be a problem with the playground editor. If I can reproduce it, I will open a bug (radar) with Apple. Thanks for pointing out the obvious. – Bill Oct 13 '14 at 23:41
  • @Bill It's easy to look past this stuff. I'd be curious to know if you can reproduce the issue with the Playground editor; I haven't had any issues with this particular syntax yet, but I've seen quite a lot of other bugs with the Playground. – Mike S Oct 13 '14 at 23:43
  • I can reproduce it. Paste in my code from above. Remove all references to x. First add back ,x=0. Then add back ,x++. This is all on the first line. My version of Xcode is the same -Version 6.1 (6A1046a). – Bill Oct 13 '14 at 23:59
  • @Bill Yep, looks like the compiler crashes just after typing `x` in `x++`. Interesting... definitely radar filing time. – Mike S Oct 14 '14 at 00:02
  • Opened as 18643692. I forgot to use Safari and had to re-enter it - silly me. Thanks for the second pair of eyes. – Bill Oct 14 '14 at 03:04