RAII's Acronym Isn't Interesting

Want to ensure cleanup?

thing := allocate_thing()
defer deallocate_thing(thing)
... more code here ...

Want to always ensure cleanup like that, without the caller needing a defer?

@(deferred_out=deallocate_thing)
allocate_thing :: proc() -> Thing {
...
}

Why not RAII?

  • In practice (but not necessarily) couples allocation/initialization together meaning that allocations are rarely bulked together and are scope-governed (which can be very poor with performance). (And I know placement `new` exists, but that isn't implicit RAII any more and doesn't solve any of the other problems)
  • C++ originally only had copy constructors which usually involved loads of implicit allocations everywhere. This lead to copy-elision optimizations and the introduction of move/ownership semantics as a way to minimize these issues.
  • Ctors and dtors usually have to assume to never fail, or require exceptions to handle the failure cases. And not everyone wants, or can even have, software exceptions.

In sum, adding RAII is not a minor thing and to make it even useful without too many flaws requires loads of extra things on top. It's not a simple construct.