Search code examples
packagecommon-lispsbclasdf

Redefining a Deleted Package (Common Lisp)


I’m using delete-package as a simple way to unintern a lot of old (user specified) variable names, before loading a project back in following certain edits to the package files. (Otherwise, new values can get pushed onto the old values for those variable names defined by the end-user.) But SBCL complains when I try to reload after the deletion.

After one-time loading an init.lisp file which sets up Quicklisp, ASDF, and installs some Quicklisp libraries, I then load the project with (progn (asdf:load-system “my-project”) (in-package :my-package)), where the project definition my-project.asd file contains

(when (find-package :my-package)
  (delete-package :my-package))

(defpackage :my-package
  (:use :cl))

(asdf:defsystem "my-project"
   …)

This all works fine on the first load, but stumbles on the second load of the project, because

*PACKAGE* can't be a deleted package: It has been reset to #<PACKAGE "COMMON-LISP-USER">.

Where is the error coming from? Can it be fixed, retaining the same functionality?


Solution

  • Create a separate package to hold the user-defined symbols, say (defpackage :us) in addition to the working package. (:use :cl) is not required since the package contains only data. Install the user symbols using (in-package :us) when loading the user files, intern into :us programmatically, or direct reference with the package prefix. Access the symbols with the package prefix. The (delete-package :us) should then work.