A question that comes up often and has also led to a few syntax suggestions 
revolves around Optionals of Collections or Sequences. Since they can easily 
arise through optional chaining, making optional sequences easier to use would 
be useful.

Much could be accomplished post-SE-0143 by adding a conditional conformance to 
Optional approximately like this:

extension Optional: Sequence where Wrapped: Sequence {
  func makeIterator() -> AnyIterator<Wrapped.Iterator.Element> {
    switch self {
    case .some(let sequence):
      return AnyIterator(sequence.makeIterator())
    case .none:
      return AnyIterator { nil }
    }
  }
}

This way, a for-in loop could handle an optional sequence just as it can a 
regular one.

Cheers,
Guillaume Lessard

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

Reply via email to