6 ms·
Zig Error Patterns
- ijustlovemath 1y agoThe website design is so pleasing, props!
- etyp 1y agoThis goes to show how Zig's language design makes everything look nicer and simpler - the `errdefer` patterns in tests are super nice! I've debugged my Zig tests with simple print debugging (or try to narrow it down to a standalone case I can use a debugger), but I'll certainly use some of these tricks in the future.
- ww520 1y agoThese are excellent tips. I especially like the debugger integration in build.zig. I used to grep the cache directory to find the exe. The integration avoids all the extra steps.
- skrebbel 1y agoWow, errdefer sounds like the kind of thing every language ought to have.
- jayd16 1y agoIs it significantly different than a try-catch block?
- ijustlovemath 1y agoone less level of indentation; reading down is happy path, seems nice!
- jayd16 1y agoHuh? But the exceptional behaviors are listed first? Would you say we should move to catch-try syntax? Seems strange to me.
- skrebbel 1y agoYeah it lets you put code that goes in "catch" all over your function body, right next to where it's most relevant. It lets you go "hm but what about errors? ah handled! ok i can forget about it" when reading a function without having to skip back and forth between the main code and the catch block.
- jayd16 1y agoBut that's just try blocks around where it's relevant except the more precise scoping of a block and the catch being after where the errdefer is before.
- masklinn 1y ago> Yeah it lets you put code that goes in "catch" all over your function body, right next to where it's most relevant. To be fair I’ve rarely seen that, usually you’ve got half the method in a giant try block and have fun untangling which errors are caught intentionally and which are side-effects.
- BradleyChatha 1y agoIf I understand the feature correctly, D has this via scope(failure) [1] ``` void func() { scope(failure) writeln("Something went wrong!"); } ``` [1] https://tour.dlang.org/tour/en/gems/scope-guards https://tour.dlang.org/tour/en/gems/scope-guards
- jiehong 1y agoNice Font! (Berkeley Mono)
- conkeisterdoor 1y ago+1 It's very nice and worth the $75 IMO. The new font builder they have gives you a lot of options to tweak/customize the typeface, which is also very nice.
- vrnvu 1y agoIt's amazing how coherent Zig's fundamental building blocks are as a programming language, everything fits together like a puzzle. This post reminds me of one of Andrew's early talks about the type system and the comptime... With the core building blocks, we can achieve elegant solutions without adding unnecessary syntax complexity.
- delifue 1y agoIn my opinion Zig is elegant except for one thing: cannot attach data to error. https://github.com/ziglang/zig/issues/2647 https://github.com/ziglang/zig/issues/2647 You have to workaround by things like passing into a pointer to error object.
- mark38848 1y agoYes also the function syntax is weird and ugly because functions aren't expressions.
- nextaccountic 1y agoWhat do you mean? pun fn f() { } ? How is this weird
- throwawaymaths 1y agoperfectly valid to do const f = fn (...) ... {...}
- nektro 1y agothis is because attaching a payload requires asking the question of who and how the memory of such a payload is managed, and Zig the language never prescribes you into a particular answer to that question.
- rkagerer 1y agoI love the formatting and coloring on this blog page, it's delightful to read. Like an old school DOS game.
- davidkunz 1y agoA 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?
- maleldil 1y ago> Are libraries typically designed to accept an optional input to store potential errors? 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.Scanner.Diagnostics https://ziglang.org/documentation/master/std/#std.json.Scann...
- AndyKelley 1y agoI'd like to note that std.json, as it currently stands, is not a good example of proper error handling. Unless you use that awkward lower level Scanner API, if you get a schema mismatch it reports some failure code and does not populate a diagnostics struct, which is painful and useless. 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 1y agoI presume sometime in the not-immediate-but-not-too-distant-future there is going to be a push to "unify" std with a bunch of the "best practices" and call them out in the documentation.
- AndyKelley 1y agoIndeed: https://github.com/ziglang/zig/issues/1629 https://github.com/ziglang/zig/issues/1629
- 1y ago
- yahoozoo 1y agoCool stuff, but the mixed casings I see here (and have in other Zig code) puts me on edge (not literally but yeah). You’ve got `addSystemCommand` then a variable named `debug_step` which has a call `dependOn`. That said, looks like most of the stdlib stuff is camel case so the snake case variables are just the authors preference.
- brianzelip 1y agoBeautiful web site!