Skip to content

Result[T] return signature in functions result.Match and resultMapErr #102

@briantliao

Description

@briantliao

Would it be possible to add Result[T] return signature in functions result.Match and result.MapErr or new similar methods?

I was playing around with error handling:

res := mo.TupleToResult(dateparse.ParseAny(dateStr))
    
if res.IsError() {
    return mo.Err[int](fmt.Errorf("dateparse could not identify format for '%s': %w", dateStr, res.Error()))
}

return mo.Ok(int(res.MustGet().Month()))

And thought

return mo.TupleToResult(dateparse.ParseAny(dateStr))
    .Match(
        func(t time.Time) { return mo.Ok(int(t.Month())) },
        func(err error) {
            return mo.Err[int](fmt.Errorf("dateparse could not identify format for '%s': %w", dateStr, res.Error()))
        },
    )

Or

return mo.TupleToResult(dateparse.ParseAny(dateStr))
    .MapErr(func(err error) {
        return mo.Err[int](fmt.Errorf("dateparse could not identify format for '%s': %w", dateStr, res.Error()))
    })
    .MapValue(func(t time.Time) { return int(t.Month()) })

Would be nice.

Based on the implementation, it would just be remove TupleToResult wrapper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeIntroduces changes that break backward compatibility or alter the public API.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions