Created
January 31, 2018 19:57
-
-
Save mikeash/04af103fbafe33f00f8c596fd6ea7b61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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