[swift-evolution] for in? optionalCollection {

Tino Heth 2th at gmx.de
Sat Feb 11 06:22:28 CST 2017


> I don't think this use case warrants a syntax change since it can already be expressed quite elegantly with
> 
> let test: [Int]? = nil
> 
> test?.forEach { i in
>     print(i)
> }

> What about just use
> 
> test?.forEach { print($0) }

This works for the simple example, but it isn't as powerful:

if let test = test {
	for i in test {
		if i == 42 {
			break
		}
	}
}

You could add
func forEach(_ body: (Element) throws -> Bool) rethrows 

but even this would be less powerful as a loop, which allows you to break, continue or return as you like.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170211/77d6cae9/attachment.html>


More information about the swift-evolution mailing list