Next: , Previous: , Up: Common Lisp Package System   [Contents][Index]

3.1.4 Defining Your Own Packages

Packages provide basic control over namespaces by controlling how the reader translates textual names into symbol objects. It is not until later, in the evaluator, that the symbol is interpreted as the name of a function or variable or whatever. It does not make sense to talk about "exporting a function" or "a variable" from a package. You can export symbols to make certain names easier to refer to, but the package system does not allow you to restrict how those names are used.

You define new packages with the macro ‘DEFPACKAGE’, which allows you to not only create the package but to specify what packages it uses, what symbols it exports, and what symbols it imports from other packages and to resolve conflicts by creating shadowing symbols.

You can define a simple package like this:

  (:use :common-lisp))

This defines a package name ‘COM.GIGAMONKEYS.EMAIL-DB’ that inherits all the symbols exported by the ‘COMMON-LISP’ package.

Packages and symbols are named with strings. However, in a DEFPACKAGE form, you can specify the names of packages and symbols with string designators. A string designator is either a string, which desigates itself; a symbol, which designates its name; or a character, which designates a one-character string containing just the character. Using keyword symbols is a common style that allows you to write the names in lowercase—the reader will convert the names to uppercase for you. You could also write the ‘DEFPACKAGE’ with strings, but then you have to write them in all uppercase because the true names of most symbols and packages are uppercase because of the case conversion performed by the reader.

To read code in this package, you need to make it the current package with the IN-PACKAGE macro:


If you type this expression at the ‘REPL’, it will change the value of ‘*PACKAGE*’, affecting how the ‘REPL’ reads subsequent expressions. If you include an IN-PACKAGE in a file that is loaded with LOAD or compiled with COMPILE-FILE, it will change the package, affecting the way subsequent expressions in the file are read. 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.

With the current package set to the ‘COM.GIGAMONKEYS.EMAIL-DB’ package, other than names inherited from the ‘COMMON-LISP’ package, you can use any names you want for whatever purpose you want.

Next: Packaging Reusable Libraries, Previous: The Three Standard Packages, Up: Common Lisp Package System   [Contents][Index]