[Review] SE-0408: Pack Iteration

In my opinion, the for loop-like syntax is unlike other places where variadic generics can be used, where the repeated value is marked by the each keyword. With the for syntax, the repeated value has to be bound to an iteration variable before use. This has the benefit of making it more obvious what value is being repeated, and feeling intuitively similar to a for loop. But it also has the disadvantage that making one variadic value syntactically "privileged" can become arbitrary. For example, there's no reason why this:

for (left, right) in repeat (each lhs, each rhs) {
    guard left == right else { return false }
}

couldn't be written like this:

repeat {
    let (left, right) = (each lhs, each rhs)
    guard left == right else { return false }
}

Going back to what ensan-hcl said earlier in this thread, the allUnwrapOrNil could be written like this:

func allUnwrapOrNil<each Element>(over element: repeat (each Element)?) -> (repeat each Element)? {
    let result: (repeat each Element)
    repeat {
        guard let element = each element else {
            return nil
        }
        each result = element
    }
    return result
}

In this case, the repeated value (each result) is an lvalue. The for loop-like syntax where repeated values are bound at the top would only allow rvalues.

3 Likes