Cl-callf

Just a quickie today. I came across this reddit post about cl-callf. It was something I had never heard of. One of the Lisp idioms is to toggle a boolean with

(setq bool (not bool))

I use it all the time but when the variable is named AVeryLongAndStupidName instead of something reasonable like bool, it can be a pain because you have to repeat the name. Of course, no true Scotsman Lisper would use such a name but occasionally one comes across such monstrosities.

It turns out that Elisp has the macro cl-callf that lets you write the idiom as

(cl-callf not bool)

There’s also the related cl-callf2. You should check out the documentation for both because they’re a bit richer than the above suggests.

I’m not sure why Emacs thinks this is a Common Lisp thing because as far as I can tell there is no callf in Common Lisp. It’s probably due to the fact that the variable can be a place rather than a simple variable in which case cl-callf does the equivalent of a setf, a Common Lisp form. In any event, it can save you some typing when using a common idiom.

This entry was posted in General and tagged . Bookmark the permalink.