A little bit unrelated, but how do people deal with the abstinence of payloads in zig errors? For example, when parsing a JSON string, the error `UnexpectedToken` is not very helpful. Are libraries typically designed to accept an optional input to store potential errors?
quantummagic|6 months ago
nextaccountic|6 months ago
Likewise, this has the disadvantage that the caller must allocate space for the error payload, even if the error is very unlikely
randyrand|6 months ago
In my C code I always allocate my error objects first, with usually 1024 bytes just for error strings.
In cases where i don’t care for error strings, i allocate 0 bytes for them.
I have a simple function to append error strings, and it checks for space. So all the code is ambivalent about whether this extra space exists.
works wonderfully.
dnautics|6 months ago
metaltyphoon|6 months ago
maleldil|6 months ago
Yes. Stdlib's JSON module has a separate diagnostics object [1]. IMO, this is the weakest part of Zig's error handling story, although the reasons for this are understandable.
[1] https://ziglang.org/documentation/master/std/#std.json.Scann...
AndyKelley|6 months ago
On the other hand the std.zon author did not make this mistake, i.e. `std.zon.parse.fromSlice` takes an optional Diagnostics struct which gives you all the information you need (including a handy format method for printing human readable messages).
dnautics|6 months ago
> Are libraries typically designed to accept an optional input to store potential errors?
https://zig.news/ityonemo/sneaky-error-payloads-1aka
if you prefer video form:
https://www.youtube.com/watch?v=aFeqWWJP4LE
The answer is no, libraries are not typically designed with a standardized convention for payload return.
davidkunz|6 months ago
jmull|6 months ago
meepmorp|6 months ago
https://github.com/ziglang/zig/issues/2647#issuecomment-2525...
hansvm|6 months ago
It's worth asking, at least a little, how often you want that in the first place.
Contrasting with Rust as an example, suppose you want Zig's "try" functionality with arbitrary payloads. Both functions need a compatible error type (a notable source of minor refactors bubbling into whole-project changes), or else you can accept a little more boilerplate and box everything with a library like `anyhow`. That's _fine_, but does it help you solve real problems? Opinions vary, but I think it mostly makes your life harder. You have stack unwinding available if you really need to see the source of a thing, and since the whole point of `try` is to bubble things up to callers who don't have the appropriate context to handle them, they likely don't really care about the metadata you're tacking on.
Suppose you want Zig's "catch" functionality with arbitrary payloads. That's just a `union` type. If you actually expect callers to inspect and care about the details of each possible return branch, you should provide a return type allowing them to do stuff with that information.
The odd duck out is `errdefer`. IMO it's reasonably common for libraries to want to do some sort of cleanup on "error" conditions, where that cleanup often doesn't depend on which error you hit, and you lose that functionality if you just return a union type. My usual workaround (in the few cases where I actually want that information returned and also have to do some sort of cleanup) is to have a private inner function and a public outer function. The inner function has some sort of `out` parameter where it sticks that unioned metadata. The outer function executes the code which might have to be cleaned up on errors, calls the inner function, and figures out what to do from there. Result location semantics make it as efficient as hand-rolled code for release builds. Not everything fits into that paradigm, but the exceptions are rare enough that the extra boilerplate really isn't bad on average (especially when comparing to an already very verbose language).
Depending on the API, your proposal of having a dedicated `out` parameter exposed further up the chain to callers might be appropriate. I'm sure somebody has done so.
Something I also do in a fair amount of my code is let the caller specify my return type, and I'll avoid work if they don't request a certain payload (e.g., not adding parse failure line numbers if not requested). It lets you write a reasonably generic API without a ton of code complexity, still allowing callers to get the information they want.
Ar-Curunir|6 months ago
This is not true, you simply need to add a single new variant to the callers error type, and either a From impl or a manual conversion at the call site