Stream: beginners

Topic: `Result` ergonomics


view this post on Zulip 947R3K (Jan 09 2026 at 00:37):

Hi! I'm wondering if there's a better way to write code like this:

my_func : Result Whatever ErrType
my_func =
     val = (func_returning_a_different_result_type
        |> Result.map_err |_| ErrType)?

In other words, I want to avoid deeply nesting my code just for the sake of error handling, so I'm using the postfix '?' operator. The function I'm calling, however, returns a Result with a different error type than the function I'm calling it from, so I have to map that error if I don't want the error type of my function to be transparent like that.

It just feels a bit clunky / verbose to me.

view this post on Zulip Brendan Hansknecht (Jan 09 2026 at 21:13):

Is this specifically asking for the old compiler and syntax, or the new compiler?

In the old compiler I think you can do:

my_func : Result Whatever (ErrType _)
my_func =
     val = func_returning_a_different_result_type ? ErrType

In the new compiler you might do:

my_func : Try(Whatever, ErrType _)
my_func =
     val = func_returning_a_different_result_type.map_err(ErrType)?

I'm not sure if we have a shorter syntax there in the new compiler currently.

Note, normally, we just nest err types instead of removing the inner error. If you want to remove the innor error, just replaec ErrType with |_| ErrType

view this post on Zulip Brendan Hansknecht (Jan 09 2026 at 21:14):

Maybe there is a more updated way, but that is what I think works currently.

view this post on Zulip 947R3K (Jan 09 2026 at 23:15):

I'm using the latest release, so new compiler I guess?

view this post on Zulip Matthieu Pizenberg (Jan 09 2026 at 23:25):

There are no releases yet for the new compiler. You’ll find some info here: https://gist.github.com/rtfeldman/f46bcbfe5132d62c4095dfa687bb9aa4


Last updated: Jan 12 2026 at 12:19 UTC