Minimal Viable Zig Error Contexts

It is plausible that swallowing canceleds isn’t a real issue!

1 Like

If you do this inside a function, how would the caller know it’s cancelled and should return?

That depends on where the “cancelled-boundary” lands. I’m assuming that it lies within the function this snippet is in. Else one would of course just handle or return Cancelled.

I write catch |err| switch (err) { so often that I wish there was a shorthand that’d eliminate the extra name in scope:

foo() catch switch {
    error.Canceled => |err| return err,
    else => // TODO
}

It could also work for other control flow constructs like:

if (optional) switch {
1 Like

Yes this seems easier for writing but I don’t like how it reads. It kind of reminds me of if let Some(f) = foo in rust, where I always have to read it thrice before getting it. This is likely because I don’t get rusty to often and because the reading order is kind of awkward and, at least for me, it’s unintuitive that f is then valid in the following scope.

I noticed that the recently rejected #5610 also seems to aim at addressing this issue. Personally, I actually think this proposal aligns with what my intuition expects.

Actually, I think this proposal enhances the consistency of the language, and for me, an increase in consistency means a decrease in language complexity rather than an increase.

1 Like

One might argue that no error sets should be swallowed? Or, that is, individual errors should only explicitly be swallowed as in switch (err) { Foo => return }

I only mean… perhaps this would expose a new decision-making challenge: “hmnn, should this error/set be declared inedible or not?” that might be an unnecessary burden, and many would just decide that none of their errors should be inedible, making it the “safe” default to declare just about everything inedible…? I know cancelation is a special case, since it (normally) requires propagation (though, that’s not swallowing), but that’s a little different, right - it’s just in the contract that cancelation has this special property. Any given error might ask for special treatment, and that just has to be learned about the error or pattern it’s a part of.

or handle AND return Canceled, as it may be.