3

Is it possible to use a with-slots-type macro with an instance of a defstruct-defined class in emacs lisp? I tried and I'm getting this error:

eieio-oset: Wrong type argument: eieio-object, "some-string", obj. 

Sample code:

(progn
  (defstruct testobj a b)
  (require 'eieio)
  (with-slots (a b)  (make-testobj :a 1 :b 2)
    (message "value: %s:"  (list a b))))
erjoalgo
  • 853
  • 1
  • 5
  • 18

1 Answers1

3

While waiting for a better answer, I wrote a with-slots macro that seems to work with defstruct instances:

(defmacro my-with-slots (class-name slots obj &rest body)
  "Bind slot names SLOTS in an instance OBJ of class CLASS-NAME, and execute BODY."
  (declare (indent 3))
  `(cl-symbol-macrolet
       ,(cl-loop for slot in slots
                 collect `(,slot (cl-struct-slot-value ',class-name ',slot ,obj)))
     ,@body))
erjoalgo
  • 853
  • 1
  • 5
  • 18
  • 2
    Instead of the `intern` which will fail if the defstruct specifies a different prefix, you can use `cl-struct-slot-value`. As you found out, currently, `slot-value` and `with-slots` are specific to EIEIO and only work on EIEIO objects. – Stefan Mar 08 '19 at 22:54
  • Thanks, edited answer. – erjoalgo Mar 12 '19 at 01:09