Expanding What's Allowed for `where` in a for-in loop?

This has been proposed before.

While I am not necessarily against what you're suggesting, if is not the right mental model. for is much more akin to a single case of a switch.

for value in structs {

is shorthand for

for case let value in structs {

. So this would not make sense unless switch where clauses gained binding capability.


This is how I would write it.

for case let (value, unwrappedValue?) in structs.with(\.optionalValue) {
extension Sequence {
  /// The sequence's elements, along with some other values.
  func with<each Value, Error>(
    _ value: repeat (Element) throws(Error) -> (each Value)
  ) throws(Error) -> some Sequence<(Element, repeat each Value)> {
    try lazy.map { element throws(Error) in
      (element, repeat try (each value)(element))
    }
  }
}
2 Likes