Skip to content

Instantly share code, notes, and snippets.

@robnadin
Created August 31, 2017 14:21
Show Gist options
  • Save robnadin/24a329a5a2a05d9468615181440d54bd to your computer and use it in GitHub Desktop.
Save robnadin/24a329a5a2a05d9468615181440d54bd to your computer and use it in GitHub Desktop.
//
// 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