Stream: announcements

Topic: New binop ? operator for mapping errors


view this post on Zulip Sam Mohr (Jan 17 2025 at 01:21):

In the next release of Roc, we'll be adding a new ? operator that works kinda like the ? suffix you know and love, but also maps over the error.

file_contents =
    File.read!("my-file.txt") ? FailedToReadFile

If the File.read! fails, it'll map over the returned Err with the provided function, in this case it'll wrap the ReadErr into an Err(FailedToReadFile(ReadErr)). This is really handy for providing quick tracing on errors when writing scripts!

You can also map the error with an inline function prior to early return:

half =
    try_to_halve(7 + 3 + 5) ? |NotEven(num)|
        HalfIsNotInt(num.to_f64() / 2.0)

view this post on Zulip Sam Mohr (Jan 17 2025 at 01:22):

When using this operator, you should not also use the suffix version of ?, a.k.a.

file_contents = File.read!("my-file.txt")? ? mapper

would be redundant. You'll probably get a type error if you do that.


Last updated: Jul 26 2025 at 12:14 UTC