7 ms·
Errors and Zig
- throwawaymaths 4y agoThe error payload thing isn't terrible. If you really really need it, it's not burdensome to add an error payload parameter pointer alongside as one of the parameters.
- skywal_l 4y agoThe error management was purposely build so you don't have to pass an additional parameters to know if your call failed or not. If you have to pass a parameter which will be then unused (null, undefined, etc) if no error was raised then you get a redundant mechanism. I think the Zig team recognize this because they still have an old issue open for it which is still planned [0]. [0] https://github.com/ziglang/zig/issues/2647 https://github.com/ziglang/zig/issues/2647
- losvedir 4y agoI'd really like something to help with that issue. I really appreciate Zig's error handling right up until you need to pass along any info other than essentially an error code. Then you have to throw away a lot of the syntax niceties and error tools that it gives you. I understand there's complications, though, with dynamic objects being returned up the stack, possibly requiring allocation, rather than a simple enum. Rust gets this right for me, at a semantic level, where an Error is just an ADT with an arbitrary payload. However, while there's something to be said for consistency in a language and treating errors as any other values that you work with via `match` and any other ordinary language constructs, I really like that Zig has some syntactic sugar for working with it. Errors are so prevalent and necessary to deal with, they don't have to be exactly like ordinary values, and it's nice that, e.g., Zig has special handling for them in `if` statements, and ways of working with them in function signatures, and automatic error unions and such.
- skywal_l 4y agoTo be fair, Rust also comes with some specific `if` constructs to deal with error management and has the equivalent of zig's `try` with `?`. I also like that their Result object has many methods to express how you want to specifically handle error case (all the variations of unwrap, unwrap_or, or, etc...).
- throwawaymaths 4y agoI'm saying workaround 2 in your link is honestly not that bad.
- von_lohengramm 4y ago> which is still planned Where are you getting that from? That proposal is not accepted.
- auggierose 4y agoTo be honest, programming in Zig sounds like a PITA to me. Building proper abstractions is difficult, and especially so if the main goal of the language is to NOT let you do this.
- Yoric 4y agoIt is my understanding indeed that Zig doesn't want you to build too many abstractions. I think that there is room in the design space for languages with few abstractions. That's also what the creators of Go intended, if I understand correctly, and that seems to work for many developers.
- auggierose 4y ago> Zig doesn't want you to build too many abstractions I don't want to build too many either. I prefer to build just the right amount of abstractions. Sounds though like Zig makes that difficult, so at least for me, it is a hard pass.
- kristoff_it 4y agoIt's a matter of perspective, I would argue that C++ makes it really hard to write the right amount, especially when you want to use overly-abstracted third party code that forces you into making a deal with the devil sometimes. But then again, it's a matter of perspective.
- auggierose 4y agoI would agree with you. I don't use C++, it is too much of a mess.
- kristoff_it 4y agoThen you can appreciate a language that tries to make sure the ecosystem does not become a mess :^)
- signa11 4y agowhy not just put all that into a function, and at the callsite just use a `try` ? that should work fn do_foo(allocator: std.mem.Allocator, ...) ... { ... } const do_foo_ret = try do_foo(...) so there is only 1 `try` instance...
- eatonphil 4y agoYou can't not `try` or `catch`. So if you made a separate function you'd just be doing all of those `try`-es line by line in that other function. const std = @import("std"); fn thing(a: std.mem.Allocator) !void { std.fmt.allocPrint(a, "", .{}); } pub fn main() !void { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); try thing(allocator); } Running `zig build-exe test.zig`: test.zig:4:23: error: error is ignored std.fmt.allocPrint(a, "", .{}); ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~ test.zig:4:23: note: consider using 'try', 'catch', or 'if' referenced by: main: test.zig:12:9 callMain: /home/phil/vendor/zig-linux-x86_64-0.11.0-dev.2213+515e1c93e/lib/std/start.zig:617:32 remaining reference traces hidden; use '-freference-trace' to see all reference traces I'll add this to the post.
- deleted 4y ago[deleted]