Skip to content

and_then doesn't re-wrap #53

@jariji

Description

@jariji
julia> Try.and_then(Ok(1)) do x
           Ok(x + 1)
       end
Try.Ok: 2

This example requires the user to wrap the value in Ok manually. An alternative API would automatically re-wrap the value in Ok, making it easier to retain the disjoint sum-type Ok{T} | Err{E} style that I often want.

Undisciplined use of and_then allows creating T | Err{E}

julia> Try.and_then(Ok(1)) do x
           x + 1
       end
2

which might be better avoided.

Is this and_then worth the risk of misuse? There are other functions that could be provided, like these in Rust.

The only situation I can think of where this would be needed is in an exotic sum type like

True | False | Unknown

where there is more than one success branch. But I'd usually represent this concept with the isomorphic

Ok(true) | Ok(false) | Err(Unknown)

which doesn't require the re-wrapping. And even with the multi-success type, users could always do manual pattern matching themselves.

Am I wrong about this? If not, what do you think about either removing and_then or documenting a requirement that the function argument return a wrapper type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions