DEFPACKAGE
adds symbols to the shadowing list before it tries to use the used packages.
226
In many Lisp implementations the :use
clause is optional if you want only to :use COMMON-LISP
—if it's omitted, the package will automatically inherit names from an implementation- defined list of packages that will usually include COMMON-LISP
. However, your code will be more portable if you always explicitly specify the packages you want to :use
. Those who are averse to typing can use the package's nickname and write (:use :cl)
.
227
Using keywords instead of strings has another advantage—Allegro provides a 'modern mode' Lisp in which the reader does no case conversion of names and in which, instead of a COMMON- LISP
package with uppercase names, provides a common-lisp
package with lowercase names. Strictly speaking, this Lisp isn't a conforming Common Lisp since all the names in the standard are defined to be uppercase. But if you write your DEFPACKAGE
forms using keyword symbols, they will work both in Common Lisp and in this near relative.
228
Some folks, instead of keywords, use uninterned symbols, using the #:
syntax.
(defpackage #:com.gigamonkeys.email-db
(:use #:common-lisp))
This saves a tiny bit of memory by not interning any symbols in the keyword package—the symbol can become garbage after DEFPACKAGE
(or the code it expands into) is done with it. However, the difference is so slight that it really boils down to a matter of aesthetics.
229
The reason to use IN-PACKAGE
instead of just SETF
ing *PACKAGE*
is that IN-PACKAGE
expands into code that will run when the file is compiled by COMPILE-FILE
as well as when the file is loaded, changing the way the reader reads the rest of the file during compilation.
230
In the REPL buffer in SLIME you can also change packages with a REPL shortcut. Type a comma, and then enter change-package
at the Command:
prompt.
231
During development, if you try to :use
a package that exports a symbol with the same name as a symbol already interned in the using package, Lisp will signal an error and typically offer you a restart that will unintern the offending symbol from the using package. For more on this, see the section 'Package Gotchas.'
232
The code for the 'Practical' chapters, available from this book's Web site, uses the ASDF system definition library. ASDF stands for Another System Definition Facility.
233
Some Common Lisp implementations, such as Allegro and SBCL, provide a facility for 'locking' the symbols in a particular package so they can be used in defining forms such as DEFUN
, DEFVAR
, and DEFCLASS
only when their home package is the current package.
234
The term KEYWORD
package. In fact, any symbol, from any package, with the appropriate name will do; the LOOP
macro cares only about their names. Typically, though, they're written with no package qualifier and are thus read (and interned as necessary) in the current package.
235
Because one of the goals of LOOP
is to allow loop expressions to be written with a quasi-English syntax, many of the keywords have synonyms that are treated the same by LOOP
but allow some freedom to express things in slightly more idiomatic English for different contexts.
236