4 ms·
Lisp does the equivalent of scope-level RAII with macros. For instance, (with-open-file (myfile "foo.txt") ...body...) will execute the body with the o
by white-flame 11y ago
Lisp does the equivalent of scope-level RAII with macros. For instance,
(with-open-file (myfile "foo.txt")
...body...)
will execute the body with the opened file bound to variable MYFILE, closing it when the end of the body is reached (or is unrolled away).
Many libraries expose this type of interface, and by Lisp's use of dynamic scoping, nested functions within the scope can access the current state of that particular scoped resource without having to pass it around manually.
In effect, RAII is programmer discipline which can manually repurpose a particular object allocation/deallocation style to support scope behavior, while languages like Lisp can simply work with scope behavior directly.