I'm trying to use cl-defmacro
, with the supposed benefit of having optional arguments specified with keywords. However, I'm not getting the outcome I would expect.
(cl-defmacro asd (a &rest body &key b)
(format "%s %s %s" a b body))
(asd 1 :b 2 "qwe")
Results in (error "Keyword argument qwe not one of (:b)")
. I.e. the forms supplied to the macro are validated against the keyword arguments, for some obscure reason. Shuffling the arguments around as (asd 1 "qwe" :b 2)
doesn't change anything.
I use the workaround of specifying &allow-other-keys
, as used in some examples:
(cl-defmacro asd (a &rest body &key b &allow-other-keys)
(format "%s %s %s" a b body))
(asd 1 :b 2 "qwe")
=> 1 2 (:b 2 qwe)
This time, the keyword arguments are eaten as part of the macro-wrapped expressions (‘body’), even though they're already collected as keyword arguments.
None of this makes sense.
I've googled far and wide and looked through code in Emacs and Doom, but apparently I'm the only one having this problem. Personally I would consider this a bug in cl-lib, though idk how it works in Common Lisp proper.