Stream: beginners

Topic: combining multiple error rows


view this post on Zulip Gabriel Pickl (Dec 05 2022 at 13:45):

Let's say I have a function with the signature Result a aError, Result a bError -> Result a [DidNotMatch]???
How can I extend the possible errors by with both aError and bError? (This is a reduced example from a parser combinator thing I'm trying)

view this post on Zulip Richard Feldman (Dec 05 2022 at 14:52):

I haven't tried it, but I'd expect this to work:

Result a err, Result a err -> Result a [DidNotMatch]err

view this post on Zulip Richard Feldman (Dec 05 2022 at 14:53):

if you give it two Results with different error types, they should get unioned

view this post on Zulip Gabriel Pickl (Dec 05 2022 at 14:58):

aah, did not think of that, but that makes sense. thank you :)

view this post on Zulip Gabriel Pickl (Dec 05 2022 at 16:06):

Hmm, this does not quite work. When I write the following:

hardcoded : Str, (Str -> Parser b err) -> Parser b [ExpectedStr Str]err
hardcoded = \value, callback ->
    @Parser \input ->
        if Str.startsWith input value then
            when Str.splitFirst input value is
                Ok {after} ->
                    @Parser next = callback value
                    next after
                Err NotFound -> crash "Did not find item that was just found"
        else
            ParseFailed (ExpectedStr value)

I get an error message that [ParseFailed [ExpectedStr Str], Parsed b Str] and [ParseFailed err, Parsed b Str] cannot be unified. If I instead use the signature

hardcoded : Str, (Str -> Parser b [ExpectedStr Str]err) -> Parser b [ExpectedStr Str]err
hardcoded = \value, callback ->
    @Parser \input ->
        if Str.startsWith input value then
            when Str.splitFirst input value is
                Ok {after} ->
                    @Parser next = callback value
                    next after
                Err NotFound -> crash "Did not find item that was just found"
        else
            ParseFailed (ExpectedStr value)

this works, but seems wrong, since I am requesting that the callback might also return ExpectedStr Str and I'm doubling up the type in the type signature

view this post on Zulip Gabriel Pickl (Dec 05 2022 at 16:07):

(the signature of Parser is Parser a err := Str -> [Parsed a Str, ParseFailed err] right now)


Last updated: Jul 05 2025 at 12:14 UTC