Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created January 31, 2018 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeash/04af103fbafe33f00f8c596fd6ea7b61 to your computer and use it in GitHub Desktop.
Save mikeash/04af103fbafe33f00f8c596fd6ea7b61 to your computer and use it in GitHub Desktop.
extension Optional: Sequence {
public func makeIterator() -> Iterator {
return Iterator(optional: self)
}
public struct Iterator: IteratorProtocol {
var optional: Wrapped?
public mutating func next() -> Wrapped? {
defer { optional = nil }
return optional
}
}
}
let x: Int? = 3
let y: Int? = nil
for i in x {
print(i)
}
for i in y {
print(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment