Skip to content

Instantly share code, notes, and snippets.

@alemar11
Forked from khanlou/NilError.swift
Created May 27, 2019 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alemar11/a40606c2afdc9f9204cfd402885600ba to your computer and use it in GitHub Desktop.
Save alemar11/a40606c2afdc9f9204cfd402885600ba 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