Search code examples
macroscommon-lisp

In Common Lisp, how to define a macro expanding to nothing (rather than `nil`)?


I want to define a macro which can comment a s-expression, for example:

I hope that

(list 1 2 (comment-macro (something))) -> (1 2)

But if I define the macro like this

(defmacro comment-macro (x))

the result of the above form is actually

(1 2 nil)

Solution

  • You cannot accomplish what you want with a regular macro because its primary value (or nil if no values are returned) will always be used.

    However, there are two common options:

    1. comment: #| ... |# - for general text

    2. feature expressions: #+(or) ... or #-(and) ... - for (temporarily?) disabling code

    You can also define your own read macros using set-macro-character and set-dispatch-macro-character.