RAII isn't about scope, it's about lifetimes that can persist beyond the instantiating scope. More like object-bound resource management.
"Lifetime" is a normative term in ISO C. In the 1999 standard, it is given as: "The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime." (6.4.2 Storage Durations of Objects)
"Lifetime" appears three times in the Index of the 1988 edition of Compilers: Principles, Techniques and Tools by Aho, Sethi and Ullman (a.k.a. the Red Dragon Book).
Lifetime, of a temporary 480
Lifetime, of an activation 391, 410
Lifetime, of an attribute 320-322, 324-329
For C++ and Rust there might not be a well defined scope since objects can be „moved“. Eg a method constructing an object can return it to the caller without the destructor being called.
You can do
with lock:
# lock is held
# lock released
with lock:
# lock is held again
Also Go's defer keyword, which is bound to the current function (defer until function returns).std::mem::forget is considered safe, as explained in its docs: https://doc.rust-lang.org/std/mem/fn.forget.html
``` (call-with-output-file some-file (lambda (out) (write 'hello out))) ```
scope(exit) foo(); // Call foo at the end of the scope
scope(success) foo(); //Call foo if all goes well
scope(failure) foo(); //Call foo if the wings fall off the plane.