Undefining a symbol macro

Viewed 86

Is it possible to undefine a symbol macro? It is possible to undefine regular variables, macros and functions using f/makunbound, but I can't seem to find a way to remove symbol macros.

I thought about removing the package they reside in altogether, but that seems like a questionable solution given the CLHS says this about symbols in deleted packages:

After this operation completes, the home package of any symbol whose home package had previously been package is implementation-dependent. Except for this, symbols accessible in package are not modified in any other way; symbols whose home package is not package remain unchanged.

So even if it works it's really just going to re-home them to some implementation dependent location.

1 Answers

Looks like your best (portable and reliable) bet is unintern:

(defvar *things* (list 'alpha 'beta 'gamma))
(define-symbol-macro thing1 (first *things*))
thing1
; ==> ALPHA
(unintern 'thing1)
thing1
; ==> The variable THING1 is unbound.

Note that this will also lose the function binding, properties &c.

Another way is to notice that define-symbol-macro "conflicts" with defvar: (defvar thing1) will remove the global symbol macro definition from thing1.

Note that it will do so silently in SBCL and via a continuable error in CLISP (for symmetry with the opposite order):

(defvar thing1)
(define-symbol-macro thing1 (first *things*))
; ==> PROGRAM-ERROR

I don't know what happens in other implementations.

Related