Stream: beginners

Topic: Try operator vs error mapping


view this post on Zulip Aurélien Geron (Jul 29 2026 at 22:21):

I just realized that f(x)? |_| Exit(1) does not mean the same thing as f(x) ? |_| Exit(1). The difference is the space before ?. Is this working as intended? It seems pretty error-prone to me.

For example, the following code causes a compilation error, but it works fine if you add a space before the ?:

main! = |args| {
    n = check_arg_count(args.len())? |_| Exit(1)
    echo!("There are ${n.to_str()} args.")
    Ok({})
}

check_arg_count = |n| { if n < 2 Try.Err(TooFewArgs) else Try.Ok(n) }

view this post on Zulip Jonathan (Jul 30 2026 at 13:07):

I think f(x)? |_| ... will be a syntax error, due to an expression following the early return, whereas with a space it is the mapping. There's an example in the all syntax file. I've gone spelunking and haven't yet found the zulip conversation where it was discussed though.

view this post on Zulip Jonathan (Jul 30 2026 at 13:09):

I think at the time it was suggested that there should be a compile warning for the likely common mixup. I'm not entirely sure of the benefit over just having map_err either.

view this post on Zulip Richard Feldman (Jul 30 2026 at 13:10):

we could also pick a different symbol for the binary operator

view this post on Zulip Richard Feldman (Jul 30 2026 at 13:11):

as I recall part of the reason we did it this way is that we had the binary operator in the old compiler before we introduced the postfix one in the new compiler

view this post on Zulip Richard Feldman (Jul 30 2026 at 13:11):

but obviously the postfix one is way more common now :sweat_smile:

view this post on Zulip Jonathan (Jul 30 2026 at 13:11):

Also true, sorry for the disparaging comment :smile:


Last updated: Aug 12 2026 at 12:35 UTC