Last active
March 14, 2018 13:18
-
-
Save StanDimitroff/89b86f358955aff580154c3306e34555 to your computer and use it in GitHub Desktop.
Throwable Optionals
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 enum ThrowableOptionalError: Error { | |
case unableToUnwrap | |
} | |
postfix operator +! | |
extension Optional { | |
public func unwrap() throws -> Wrapped { | |
switch self { | |
case .some(let value): | |
return value | |
default: | |
throw ThrowableOptionalError.unableToUnwrap | |
} | |
} | |
static postfix func +!(value: Optional<Wrapped>) throws -> Wrapped { | |
return try value.unwrap() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment