Skip to content

Comment on Zig: Upcoming release postponed two more weeks and lacks async functionsparent

Comments

You can `errdefer/defer if` inside the blk, then x = blk:{}; defer use(x)` outside the block.

That wasn't a request for help (I have debugged my reference counts already), but I thank you nonetheless.

You can do as you say, but that winds up being a lot of non-enforced boilerplate. And you can get subtle errors if you miss the defer at the wrong scope (ask me how I know this ... actually, better yet, please don't as it will give me flashbacks :) ). And neither the language nor compiler can help you. This contrasts with, say, Rust where reference counts or locks can be enforced by the compiler and simply never go wrong.

defer/errdefer is obvously vastly better than C. My codebase was painful in C. Zig made it tractable, but it was hardly pleasant.

Unfortunately, I really don't have a good suggestion as to what Zig should do instead. defer/errdefer is a minimum, but it's not clear what a single, better step further actually would be. Most solutions in other languages wind up with RAII and that invokes a nightmare of cascading design decisions through a programming language that Zig very much does not want to follow.

It will be interesting to see where async/await finally lands. I think that will have some component of a "slightly better" solution.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.