47 ms·
I wish I could pray and convince language author to please give us RAII in Zig. That is the only thing I want presently - I truly detest writing `defers` after
by lenkite 2y ago
I wish I could pray and convince language author to please give us RAII in Zig. That is the only thing I want presently - I truly detest writing `defers` after my experience in Golang. If Zig wants trash cleanup absolutely explicit, then please at-least give us a C# like `using` or Python `with` statement. I will move to using Zig tomorrow and praise it to the heavens.
- jcaguilar 2y agoDo you mind explaining why defer is a problem in Go? I only have experience with Java’s try-with-resources which I like a lot, it looks like C# using is similar.
- throwaway_qiwuw 2y agoI’m not the commenter but I too am not a fan. 1. You cannot remotely easily wire them in a SIGINT scenario. I have done it, it was a load of plumbing and I wrote an equivalent to Java’s shutdown hook and tied it to sigterm and sigint then called that same hook in the normal shutdown path to simulate the defer call cycle. 2. “using” or “with” in other languages is nice and explicit about when it runs and you can easily run it before your function exits. In the case of go you need to wrap some function to force this same behavior in Go and it looks very forced. Defer is fine, especially if you’re writing something with a simple life cycle. I just end up using it less when I need to support cleanup functions being called no matter what in a graceful shutdown.
- jcaguilar 2y agoInteresting, I can see those 2 issues.
- rofrol 2y agoad.2 defer in go is function-scoped, but in zig block-scoped, so you can create block in zig to defer earlier?
- vrighter 2y agogo's defer is braindead because of its scoping. Zig's defer is block scoped, so you can actually use it much more easily.