trifork / TriforkSwiftExtensions

Generic Trifork Swift Extensions
MIT License
8 stars 6 forks source link

Result closures #56

Closed ghost closed 5 years ago

ghost commented 5 years ago

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)")
}
trifork-bot commented 5 years ago
1 Warning
:warning: Big PR

Generated by :no_entry_sign: Danger

ghost commented 5 years ago

@tkctrifork TriforkSwiftExtensions will not be triggered, so can safely be merged