[swift-evolution] Sequence/Collection Enhancements
Guillaume Lessard
glessard at tffenterprises.com
Thu Feb 16 21:49:12 CST 2017
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
More information about the swift-evolution
mailing list