Last active
February 4, 2020 11:11
-
-
Save freak4pc/ead00eba20d03b304b5ff70084e1e8f7 to your computer and use it in GitHub Desktop.
Optional Unwrapping with Closures
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
public protocol OptionalType { | |
associatedtype Wrapped | |
var value: Wrapped? { get } | |
} | |
extension Optional: OptionalType { | |
public var value: Wrapped? { | |
return self | |
} | |
@discardableResult | |
public func then(_ closure: (Wrapped) -> Void) -> Optional { | |
if case .some(let item) = self { | |
closure(item) | |
} | |
return self | |
} | |
public func otherwise(_ closure: () -> Void) { | |
if case .none = self { | |
closure() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Allows doing things like
Or