Stream: beginners

Topic: Can I accumulate multiple error types with try?


view this post on Zulip Ian McLerran (Jan 21 2025 at 15:04):

I think I'm probably miss understanding how to accumulate errors, but I have an error message I can't quite figure out.

What confuses me here is that it is complaining about the annotation of my error type, but I actually replaced my error type annotation with a []_ to try to get it to type check. Here is the error message:

Something is off with the body of the to_str definition:

22│  to_str : List U16 -> Result Str []_ #[BadUtf16 Utf16Problem, BadUtf8 Utf8.Utf8Problem]
23│  to_str = |utf16|
24│      codepoints = to_codepoints(utf16)?
                      ^^^^^^^^^^^^^^^^^^^^^

This returns an Err of type:

    [
        Err [BadUtf16 Utf16Problem],
        Ok Str,
    ]

But the type annotation on to_str says it should be:

    Result Str [BadUtf8 {
        index : U64,
        problem : Utf8ByteProblem,
    }]

The relevant code is as follows:

to_codepoints : List U16 -> Result (List U32) [BadUtf16 Utf16Problem]

to_str : List U16 -> Result Str []_ #[BadUtf16 Utf16Problem, BadUtf8 Utf8.Utf8Problem]
to_str = |utf16|
    codepoints = try to_codepoints(utf16) # annotation above
    codepoints # List U32
    |> Utf8.from_codepoints # List U32 -> List U8
    |> Str.from_utf8 # List U8 -> Result Str [BadUtf8 Utf8.Utf8Problem]

view this post on Zulip Brendan Hansknecht (Jan 21 2025 at 16:30):

Does just _ work instead of []_?

view this post on Zulip Ian McLerran (Jan 21 2025 at 17:13):

Same thing. And removing the type annotation entirely doesn't work either. It produces a slightly different error.


Last updated: Jul 06 2025 at 12:14 UTC