[swift-evolution] for in? optionalCollection {
André “Zephyz” Videla
andre.videla at gmail.com
Sat Feb 11 06:43:24 CST 2017
Ah I see you point a bit better.
But I don't agree with your example, since it can be easily expressed with
test?.prefix(while: { $0 != 42}).forEach { i in
print(i)
}
But arguing about examples is besides the point, I would like to stop here.
I have a question for you. How do you think we could use this pattern in the generalised situation:
if condiition {
for thing in condition {
}
}
for example
enum Result<T> {
case success(T)
case failure(Error)
}
let result: Result<[Int]> = .success([0,1,2,3])
if case .success(let arr) = result {
for e in arr {
print(e)
}
}
Maybe it would be nice to write something along the lines of
for e in case .success(let arr) = result {
}
or even
for e in arr where .success(let arr) = result {
}
therefore the "in?" syntax would be sugar for
for i in array where let array? = test {
}
> On 11 Feb 2017, at 13:22, Tino Heth <2th at gmx.de> wrote:
>
>> 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/44859752/attachment.html>
More information about the swift-evolution
mailing list