Created
November 29, 2019 18:08
-
-
Save 0xLeif/ba25666644ff20255d0a196c4f23de98 to your computer and use it in GitHub Desktop.
Optional Use
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
import Foundation | |
public extension Optional { | |
func use(_ closure: (Wrapped) -> Void) { | |
guard let unwrappedSelf = self else { | |
return | |
} | |
closure(unwrappedSelf) | |
} | |
} | |
var o: String? = nil | |
o.use { | |
print($0) | |
} | |
o = "534" | |
o.use { | |
print($0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment