#71: Result in Swift

Result is a compact way to represent either a success value or an error.

func parse(_ text: String) -> Result<Int, Error> {
    if let value = Int(text) {
        return .success(value)
    } else {
        return .failure(ParseError.invalidNumber)
    }
}

This is especially nice when working with asynchronous APIs or pipelines where you want to pass success and failure through the same channel.

It also avoids the awkwardness of returning both an optional value and a separate optional error.


I'm also on Twitter and GitHub.

Subscribe via RSS.