Created
August 31, 2017 14:21
-
-
Save robnadin/24a329a5a2a05d9468615181440d54bd 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
// | |
// Optional+Unwrapped.swift | |
// | |
public enum UnwrapError: Error { | |
case failedToUnwrap(file: StaticString, function: StaticString, line: UInt) | |
} | |
extension Optional { | |
public func unwrapped(error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Wrapped { | |
switch self { | |
case .none: | |
throw error ?? UnwrapError.failedToUnwrap(file: file, function: function, line: line) | |
case .some(let value): | |
return value | |
} | |
} | |
public func unwrap(error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, then: (Wrapped) -> Void) throws { | |
switch self { | |
case .none: | |
throw error ?? UnwrapError.failedToUnwrap(file: file, function: function, line: line) | |
case .some(let value): | |
then(value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment