Hi forum,
What would be the reasonable way to create a function-local variable with indefinite extent; i.e. that keeps its value between invocations of the function? The "function-local" part isn't really critical, of course, as long as it doesn't dirty the namespace otherwise. My naive implementation, I guess, would look something like this:
What would be the reasonable way to create a function-local variable with indefinite extent; i.e. that keeps its value between invocations of the function? The "function-local" part isn't really critical, of course, as long as it doesn't dirty the namespace otherwise. My naive implementation, I guess, would look something like this:
(defmacro let-indef (var init-form &body body)
(let ((real-var (gensym)))
`(locally
(declare (special ,real-var))
(unless (boundp ,real-var) (setf ,real-var ,init-form))
(symbol-macrolet ((,var ,real-var))
,@body))))
However, it doesn't seem to work. When I try to use a variable declared as such in SBCL, I get the error "The variable #:G0 is unbound.". I guess "#:G0" is my gensymed special symbol, but I don't understand why it is unbound, seeing how I assign it explicitly when unbound. Also, I'm unsure how this technique would interact with compilation (do gensymed specials work?). Does anyone know?