[swift-evolution] for-else syntax

Dimitri Racordon Dimitri.Racordon at unige.ch
Fri Feb 3 03:27:47 CST 2017


Talking of Python, Swift is not Python and the argument not to implement a feature because its semantics conflict with the semantics of a similar looking feature in another language is bogus. I don’t see the Python for … else being different (and having looked it up to see what you all were talking about, my opinion is that the Python for … else is a disaster) as being a legitimate con to this cleaner and more logical idea in Swift.

Swift is not Python, but Python programmers do program in Swift. Yeah they can look up the documentation and see that the semantics is not the same, but that is definitely error-prone.

While this kind of construct happen quite often, I am not for changing the syntax of a for-loop. To be honest, I don’t think that "for { .. } else { .. }” is so clear intuitive and I think that I would choose most if not all of the other constructions that have been proposed to express the OP’s problem.

Maybe we can add a new parameter "otherwise" to the forEach method

[1,2,3,4].forEach({
// do something
}
, otherwise: {
// do something if it is an empty array
})

The problem with this approach is that it would prevent the body of the for-loop to be expressed with a trailing closure.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170203/c3fa2133/attachment.html>


More information about the swift-evolution mailing list