4 ms·
It's actually rare that I end up needing extra stuff attached to errors in zig. When I do, I end up thinking of a higher unit of work where I can stick some con
by laserbeam 3y ago
It's actually rare that I end up needing extra stuff attached to errors in zig. When I do, I end up thinking of a higher unit of work where I can stick some context data in case of errors.
For example, the article mentions a JSON parser. In zig I'd end up writing a class with minimal state and a few methods. I'd call parser.parse(...), and if that goes bad I just fill a parser.errdata field before returning an error. This is similar to the old errno.h way of doing it in C, but it's not global.
That's the pattern I use, if I need detailed error information, I make sure to provide a pointer where they can get written. And it turns out I haven't needed that too often...
- throwawaymaths 3y agoYou should consider using options error payloads. Saw an article on zig.news Benefit is if you don't want the payload, the code to generate the payload gets compiled out.
- skybrian 3y agoThis seems to be it: https://zig.news/ityonemo/sneaky-error-payloads-1aka https://zig.news/ityonemo/sneaky-error-payloads-1aka It looks like it would be useful, but relies on a naming convention? Hopefully everyone picks the same field names.
- throwawaymaths 3y agoOr, read the docs on the function you're calling?
- skybrian 3y agoI was thinking more about what happens if you’re combining already-written code that uses different naming conventions, or even different types. Suppose you get an error in one format and want to pass it on in a different format? Writing a converter probably isn’t so hard, but it’s tedious, like having multiple kinds of strings. But it’s probably too soon to worry about that when Zig doesn’t have a package manager yet.
- dns_snek 3y ago> But it’s probably too soon to worry about that when Zig doesn’t have a package manager yet. Just fyi in case you haven't seen the news, It launched with 0.11 release a couple of days ago.
- throwawaymaths 3y agoAgreed. Probably tedious. But straightforward, unlike say, multiple inheritance, and, likely to be rare.
- throwawaymaths 3y agoThe article was updated: https://zig.news/ityonemo/error-payloads-updated-367m https://zig.news/ityonemo/error-payloads-updated-367m