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 <[email protected]> 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.

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to