6 ms·
When people talk about RAII in relation to Zig I think they mean something slightly different than RAII, but then the conversation starts to become about what i
by AndyKelley 5y ago
When people talk about RAII in relation to Zig I think they mean something slightly different than RAII, but then the conversation starts to become about what is the definition of RAII rather than whether the Zig language is lacking a certain kind of useful abstraction.
Examples:
[1]: https://news.ycombinator.com/item?id=29506814 https://news.ycombinator.com/item?id=29506814
[2]: https://gist.github.com/andrewrk/190170bc1441839644c3f15725a22096 https://gist.github.com/andrewrk/190170bc1441839644c3f15725a...
- nikki93 5y agoNot saying this means you actually need to add constructors and destructors, but I think the main tricky bit is when you have eg. a grow / shrinkable array and want to call something on it that removes elements and it should be calling destructors on those. AFAICT there's no clear place to add a `defer` that makes it happen at the right time at the lexical site those elements are originally added. I think this is where the main RAII complexity comes from vs. the lexical scope local variable scenario which is definitely handled by `defer`. All that said, not an argument for having that, just wanted to nuance it. I think not having it and keeping the language focused on its priorities can be good, for example.
- tialaramex 5y agoSurely the only part you need for this scenario is a way to dispose of whatever resources are behind the elements? So if you have interfaces a Disposable interface is enough, the code can determine that these elements are Disposable and dispose of them when they're removed. That's quite a lot less intense than needing a language-level concept of destructors. If you're writing a low-level language it might well be better to hook this in explicitly like Rust's Drop trait (the Drop trait is a langitem in Rust, it must exist), but in high-level languages I think you'd get almost all the value from a Disposable interface even if there is no actual language support provided.
- Spex_guy 5y agoHaha yeah this is a good insight. "Resource Acquisition Is Initialization" actually has nothing to do with resource acquisition or initialization in most peoples' minds, instead people (including me) tend to think of it as "Resource Release Is Implicit Object Lifetime Termination". RRIIOLT doesn't really have the same ring to it though xD