I'm trying to understand why this doesn't work, and how to make it work:
(defvar test/var-1 nil)
(defvar test/var-2 nil)
(defun make-setter (var-to-set)
(lambda (val-to-set)
(setq var-to-set val-to-set)))
(defun do-the-thing ()
(let ((the-val 5))
(funcall (make-setter test/var-2) the-val)))
(do-the-thing) ;; After this function is called, I'd expect
;; test/var-2 to have the value 5, but it's nil.
I assume the problem is that the call to setq
is setting the value of the symbol var-to-set
, overwriting the previous value of test/var-2
with 5. How do I pass in the name of the variable to set and and set the value of that variable?
(make-setter test/var-2)
is passing the value oftest/var-2
, i.e.,nil
. – npostavs Feb 11 '17 at 1:00(elisp) Setting Variables
– npostavs Feb 11 '17 at 1:02make-setter
should be a macro, if you expectVAR-TO-SET
not to be evaluated beforemake-setter
gets hold of it. You also have a problem in expecting a relationship between that local variable for the function and the other variable of the same name that is bound by the lambda form. There are really several things wrong with the code. You would do well to read some of the Emacs Lisp Intro manual, (An Introduction to Programming in Emacs Lisp) available by doingC-h i
. – Drew Feb 11 '17 at 2:42