Skip to content

Instantly share code, notes, and snippets.

@StanDimitroff
Last active March 14, 2018 13:18
Show Gist options
  • Save StanDimitroff/89b86f358955aff580154c3306e34555 to your computer and use it in GitHub Desktop.
Save StanDimitroff/89b86f358955aff580154c3306e34555 to your computer and use it in GitHub Desktop.
Throwable Optionals
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