Our next obscure Lisp command for newcomers arriving from C++ is delete-package. To begin, I’ll discuss what a package behaves like from the perspective of C++.
A package is a bit like a C++ namespace, but with a few extra twists. In C++, a namespace is essentially a name-mangling tool. It allows the programmer to create symbols without worrying too much about symbol name collisions. In Lisp, a package has this quality, but also other properties. First of all, a Lisp package is a real obect in the language, not simply a compiler directive for symbol naming. The package is, very broadly, a symbol container. Secondly, packages can inherit from other packages, and can therefore inherit symbols or override them as necessary. Don’t confuse a package with a class, though, as you can define many classes or no classes in a package, and there isn’t an equivalent to polymorphism in the syntax of package symbol resolution, the way there is with class hierarchies and generic functions.
So, what does delete-package do? Well, perhaps unsurprisingly, it deletes the package. The result is that the classes and functions defined in the package are no longer visible in the Lisp environment. Once again, the need for this is due to the fact that Lisp programs run in a Lisp environment, and not as self-contained things that are compiled and then executed.
For the newcomer, when might you need delete-package? Most obviously, during development. If you’re working in a package, and you’ve defined some generic functions, only to decide later that you need to change the interface of a generic function and the methods that implement it, your Lisp instance will complain when you try to re-load the package, because redefining the generic function will bring it into conflict with the implemented methods. By calling delete-package before reloading the modified file, you delete the changes that the package made to the Lisp system, and so can safely load the file. Other cases where you might need to delete the package before loading it include when you’ve got a defconstant on a list (see the earlier discussion there), or when you’ve used defvar in the package. Really, any time when the state of the package might modify the effect of loading the package again.