Skip to content

Adding a fold method #268

@deanWombourne

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?

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