Q: how can I pass an argument to an object constructor that won't be interpreted as a slot?
I am creating an instance of a class. The value of one of its slots depends on another slot and auxiliary information not contained in the class itself.
I have written an
after method for
initialize-instance, as in
the toy example below:
(defvar testvar '((a . "a value") (b . "b value"))) (defclass testclass () ((slot-1 :initarg :slot-1 :initform nil :accessor get-slot1) (slot-2 :initform nil :accessor get-slot2)) "Toy class.") (cl-defmethod initialize-instance :after ((test testclass) &key) (with-slots (slot-1 slot-2) test (setf slot-2 (cdr (assq slot-1 testvar)))))
In the example, the value of
slot-2 depends on the value of
slot-1, which requires looking up something in auxiliary alist
(setq var1 (testclass :slot-1 'a) var2 (testclass :slot-1 'b)) (get-slot2 var1) ; => "a value" (get-slot2 var2) ; => "b value"
That's all well and good, but: to do this, I have to create and
access a global variable
testvar. How can I do this without
using a global variable? How can I pass another argument to the
testclass constructor (or
initialize-instance) that contains
the relevant alist without it being interpreted as a slot value?
(I'm exploring elisp's OO facilities, so there's a decent chance I'm missing something obvious.)