Skip to content

Adding a fold method #268

Description

@deanWombourne

I have a few situations where I need to convert a Result to a common type, and looking at other languages fold seems to exist.

Essentially, this:

public extension Result {

    /// Convert either a success or failure into type `U`
    public func fold<U>(success: (Value) throws -> U, failure: (Error) throws -> U) rethrows -> U {
        switch self {
        case .success(let value): return try success(value)
        case .failure(let error): return try failure(error)
        }
    }
}

Good idea / bad idea?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions