[swift-evolution] else clause for loops like that in Python

Brent Royal-Gordon brent at architechies.com
Tue Dec 8 19:13:29 CST 2015

> What about searching for something in an array? The not found code would go in the nobreak clause.

For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.

If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.

I’m not saying it’s a useless feature, just that *I* can’t imagine using it.

Brent Royal-Gordon

More information about the swift-evolution mailing list