4 ms·
Can you expand on how errdefer works in zig? I'm not familiar.
by infogulch 2y ago
Can you expand on how errdefer works in zig? I'm not familiar.
- Jtsummers 2y agohttps://ziglang.org/documentation/master/#errdefer https://ziglang.org/documentation/master/#errdefer defer always executes on scope exit, errdefer executes on an error exit. In principle, this is similar to the logic of a try/catch/finally: try { // whatever } catch { // errdefer would belong here } finally { // defer would happen here }
- loeg 2y agoZig has a special / compiler-known ADT for "value OR error". This is similar to Result<T,E> in Rust. Or in C++, e.g., folly::Expected<T,E>. The Zig one is so special and compiler-blessed that there is special syntax for defer blocks that only run when the function return is an error variant of that result ADT -- errdefer.