3 ms·
The problem is actually remembering to use `using`, `finally`, `with-foo`, whatever. With RAII, I don't need to remember - forgetting to free is literally impos
by catnaroek 10y ago
The problem is actually remembering to use `using`, `finally`, `with-foo`, whatever. With RAII, I don't need to remember - forgetting to free is literally impossible.
- pjmlp 10y agoRAII only works with global and stack allocated data. You also need to remember to use RAII in every other case. You might also need to implement RAII handle classes in specific use cases.
- catnaroek 10y agoOnce for every struct/class, which is much better than once for every individual resource used.
- pjmlp 10y agoYou don't need to do it for every individual resource used, specially if the language has support for meta-programming. I guess these FP patterns are yet to be widely known.
- gpderetta 10y agoThe biggest difference between RAII and other approaches based on higher order functions and closures is that RAII is bound to object lifetimes, while other approaches are bound to scopes. They compose differently.
- lmm 10y agoAbsolutely right, and it absolutely is immensely valuable. It's perfectly possible to do the same thing in a higher-level language though - if you only expose with-file then you have the same safety guarantees as RAII. If you only expose composable commands and a way to execute them (i.e. a free monad approach - doobie is a good example) then you can get even better guarantees than are practical with RAII (there's no way to do the equivalent of "&myFile").
- catnaroek 10y ago`with-file` doesn't guarantee anything about users who forget to use it. With RAII, only the implementor needs to remember to use it, users can't “forget”, because there's nothing to remember. > It's perfectly possible to do the same thing in a higher-level language though - if you only expose with-file then you have the same safety guarantees as RAII. It's still less sophisticated than desirable. In Rust, objects can be moved to a different environment, so cleanup need not follow a strict LIFO discipline.