Created
May 24, 2018 15:57
-
-
Save mikeash/29e06d8db3f5399e9eb01159fc41b617 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
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