Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created July 5, 2017 00:01
Show Gist options
  • Save khanlou/e4b5db71a6a18903d8a78edd04068780 to your computer and use it in GitHub Desktop.
Save khanlou/e4b5db71a6a18903d8a78edd04068780 to your computer and use it in GitHub Desktop.
public struct NilError: Error, CustomStringConvertible {
let file: String
let line: Int
public init(file: String = #file, line: Int = #line) {
self.file = file
self.line = line
}
public var description: String {
return "Nil returned at " + (file) + ":\(line)"
}
}
extension Optional {
public func unwrap(file: String = #file, line: Int = #line) throws -> Wrapped {
guard let result = self else {
throw NilError(file: file, line: line)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment