I miss a closure handling when working with the Result type of Swift 5. Inspired by BrightFutures, I came up with a solution that makes it possible to handle success and failure like this:
func doSomething() -> Result<Int, DoSomethingError> {
let result: Result<Int, DoSomethingError>
if Bool.random() {
result = .success(Int.random(in: 0...20))
} else {
result = .failure(.somethingFailed)
}
return result
}
doSomething()
.success { (value: Int) in
print("Yay, we succeeded: \(value)")
}.failure { (error: DoSomethingError) in
print("Shit, we failed: \(error)")
}
I miss a closure handling when working with the
Result
type of Swift 5. Inspired by BrightFutures, I came up with a solution that makes it possible to handlesuccess
andfailure
like this: