3 ms·
I added something like this to TXR Lisp in 2015. with-objects binds variables to objects, like let. When it terminates, the GC finalizers of the objects (if th
by kazinator 3y ago
I added something like this to TXR Lisp in 2015.
with-objects binds variables to objects, like let. When it terminates, the GC finalizers of the objects (if they have any) are invoked.
1> (with-objects ((x [finalize (list 1 2 3)
(lambda (obj)
(format t "bye-bye ~s\n" obj))]))
(format t "inside with-objects: x is ~s\n" x))
inside with-objects: x is (1 2 3)
bye-bye (1 2 3)
Mainly this is meant to be used with OOP structs, where you can declare finalizers.
1> (defstruct widget ()
(:fini (me) (put-line `@me's finalizer`)))
#<struct-type widget>
2> (with-objects ((w (new widget)))
(put-line `have @w`))
have #S(widget)
#S(widget)'s finalizer
t
It provides a kind of RAII that can be useful from time to time.