Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created May 24, 2018 15:57
Show Gist options
  • Save mikeash/29e06d8db3f5399e9eb01159fc41b617 to your computer and use it in GitHub Desktop.
Save mikeash/29e06d8db3f5399e9eb01159fc41b617 to your computer and use it in GitHub Desktop.
func ??<T>(lhs: T?, rhs: @autoclosure () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
func ??<T>(lhs: T?, rhs: () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
enum GimmeA: Error {
case B
}
func f(_ a: Int?, _ b: String?) throws {
let a = a ?? fatalError("Gimme an A!")
let b = try b ?? { throw GimmeA.B }
print(a, b)
}
do {
try f(42, "forty-two")
try f(42, nil)
} catch {
print("Threw", error)
}
do {
try f(nil, nil)
} catch {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment