5

Q: how do I teach setf about new places it can set?

In common lisp, one uses the macro defsetf to tell setf about new places it can set.

In elisp, that macro exists in in the cl library, but, as far as I can tell, has not been ported into the updated cl-lib library. However, we're not supposed to use the old cl library:

Since the old cl.el does not use a clean namespace, Emacs has a policy that packages distributed with Emacs must not load cl at run time. (It is ok for them to load cl at compile time, with eval-when-compile, and use the macros it provides.) There is no such restriction on the use of cl-lib. New code should use cl-lib rather than cl.

Does defsetf (or a substitute) exist somewhere else? How else should I tell setf about new places?

1
  • The docstring for defsetf in cl.el equates different forms of defsetf with gv-define-simple-setter and gv-define-setter, which are in turn described as easy-to-use substitutes for gv-define-expander.
    – Basil
    Nov 12, 2017 at 22:36

2 Answers 2

4

Where does it say that you're not supposed to use library cl.el? That would be silly (IMHO). The text you quote says that code distributed with GNU Emacs must not load cl at runtime. That does not say that you should not use cl.el (at runtime or any other time).

Some people don't want to load all of cl.el at runtime. That's one reason cl-lib.el was created: as a subset of cl.el. (Another reason was to provide the prefix cl- more systematically, as mentioned in the text you quote.)

But defsetf is a macro. You generally do not need a macro at runtime. You typically need it only at byte-compile time.

This is all you need: (eval-when-compile (require 'cl)).

2
  • Thanks! Do you know what the policy is about using cl with packages that might be distributed via elpa or melpa? And do you happen to know if the rest of cl is going to get ported into something like cl-lib (re: "New code should use cl-lib rather than cl")?
    – Dan
    Nov 13, 2017 at 19:09
  • Hi Dan. No, I really don't know these things. Your best bet is to ask [email protected]. As many of my libraries try to support multiple Emacs releases, including some that predate cl-lib.el, I generally avoid using CL functions in my code, but I use CL macros and use eval-when-compile to load their definitions at byte-compile time.
    – Drew
    Nov 13, 2017 at 22:40
5

Drew's answer is correct, but it should also be mentioned that the reason cl-lib doesn't include a cl-defsetf (or cl-setf) is because the setf machinery has been moved to gv.el. The docstring of defsetf suggests gv-define-simple-setter and gv-define-setter as alternatives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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