Using Emacs

To set the slot of a structure, I believe the elisp manual says to use setq along with the accessor function for the slot. Though I am having trouble.

The following code results in an error on the fourth line.

(require 'cl)
(cl-defstruct foo name)
(setq bar (make-foo :name 'john))
(setq (foo-name bar) 'jim)


eval: Wrong type argument: symbolp, (progn "Access slot \"name\" of `foo' struct CL-X." nil (or (progn nil (and (vectorp bar) (>= (length bar) 2) (memq (aref bar 0) cl-struct-foo-tags) t)) (signal (quote wrong-type-argument) (list (quote foo) bar))) (aref bar 1))

Also there is no cl tag to use for the Emacs Common Lisp Emulation Library... don't know what else to put it under, just going to put it under common-lisp for someone to help me find the right tag.

  • 3
    You need to use setf instead of setq. – syohex Sep 28 '16 at 0:28
  • Oh, I was not seeing a difference... though now it seems perfectly clear, thank you. – lookyhooky Sep 28 '16 at 2:50
  • @syohex, you should make that an answer. – Aaron Harris Sep 28 '16 at 14:35
  • Answered as answer – syohex Sep 29 '16 at 2:36

Use setf instead of setq.

(setf (foo-name bar) 'jim)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.