Error payloads in Zig

srcreigh.ca

67 points by srcreigh 8 hours ago


latch - 3 hours ago

It wasn't clear from the examples, and the gist doesn't have a `deinit` method, so what happens if an error needs to own data?

> Here, sqlite.ErrorPayload.init saves 500 bytes of error message from sqlite

Who owns those 500 bytes and where are they being freed?

umairnadeem123 - an hour ago

the 'error payload' pattern feels like a good compromise between (a) enums and (b) full exceptions, esp when you want to preserve structured context. in zig specifically, do you find yourself standardizing payload shapes per module, or does it devolve into ad-hoc structs? would be interesting to see guidance on when to box/allocate vs keep payload trivially copyable.

twhitmore - 3 hours ago

Does the Zig language not have useful exceptions/stacktraces that can be propagated out?

At a high level, all non-trivial programming is composition. And (see principle of encapsulation) the vast majority of errors shouldn't be recovered and just need to be propagated out.

Then, to be useful, errors need enough information to be diagnosed or investigated. It seems like this should have been a straight-forward requirement in the language design.

scuff3d - 5 hours ago

Continues to be a point of annoyance that Zig doesn't properly support payloads in errors.

ozgrakkurt - 2 hours ago

Why need error payloads when:

You can do validation at user interface and report actual nice errors.

And if something happens after that, you can save the stack trace into somewhere so the developer can see it. And you report unexpected error to user as that is exactly that

grayhatter - 3 hours ago

Alternative title; instead of learning a new language, I adapted it into something I'm more familiar with.

Depending on your language preference; Zig has issues[citation needed], but 1) it's still version 0 and 2) and this is the important part: who cares?

I get you have a pattern and shape you like a lot. But there's less value in that existing shape, than there is in being mildly uncomfortable, and expanding your world view and trying things in ways you wouldn't normally do.

If you tried it and didn't like it, cool, go back to the language you're already used to. But for everyone else. I'd encourage you to try doing things "wrong" for a while and seeing if you can't learn something from forcing yourself to do so.

Something especially true for a language that HN likes to pretend is just a toy and can't compete with [ language you already decided won ]

deepriverfish - 5 hours ago

page is dead