Hacker News new | ask | show | jobs
by pod_krad 536 days ago
>The idiomatic way to handle that is with some combination of `defer` and `errdefer`. Then, in code, there are no lines between alloc/free, and if a try would exit before the free then you would instead first free anyway.

Do I understand correctly, that talking about idiomatic way is the same as talking about programming style, but not about language semantic?

1 comments

Correct. Much like in Python or C, you can explicitly handle every error/break/early-return condition and manually free the resource. Each language offers a tool to make that easier (and more likely to be correct, especially as the code changes) in most cases though. In Python it's try/finally, in C it's `goto ERR`, and in Zig it's defer/errdefer.
So, you can't rely that someone will follow that style in Zig. That is my point.