All Manuals > LispWorks® User Guide and Reference Manual > 33 The COMMON-LISP Package

defpackage Macro

Summary

Extended to add a way of specifying the default used packages, and control package name conflict resolution.

Package

common-lisp

Signature

defpackage defined-package-name {option}* => package

option ::= (:add-use-defaults) | (:local-nicknames (local-nickname actual-package-name)*) | standard-option

Arguments
defined-package-name
A string designator.
local-nickname
A string or a symbol.
actual-package-name
A string or a symbol.
standard-option
The standard keyword options to defpackage.
Values
package
A package.
Description

The macro defpackage is as defined in the ANSI standard with standard-options, plus the addition of the :add-use-defaults and :local-nicknames options. However, the standard explicitly declines to define what defpackage does if a package named defined-package-name already exists and is in a state that differs from that described by the defpackage form.

Therefore an extension has been written that allows you to select between alternative behaviors. See *handle-existing-defpackage* for full details.

When either the standard :use option is omitted or :add-use-defaults is supplied as an option (with any value), then the package defined-package-name is defined to inherit from the following packages (as well as any explicitly specified by the :use option):

Otherwise, defined-package-name is defined to inherit from the packages specified by the :use option only.

If :local-nicknames is supplied as an option then defined-package-name is defined to have the specified local-nicknames for the corresponding actual-package-names.

Using :local-nicknames in defpackage is equivalent to doing the defpackage without :local-nicknames, and then calling add-package-local-nickname for each pair in the list with defined-package-name as the package-designator, except that DEFPACKAGE does some checking and may give an error before starting to make any changes. See add-package-local-nickname for details.

Examples

Using :add-use-defaults:

(defpackage "MY-PACKAGE" (:use "CAPI")
                         (:add-use-defaults t))
 
(package-use-list "MY-PACKAGE")
=>
(#<PACKAGE COMMON-LISP> #<PACKAGE LISPWORKS>
 #<PACKAGE HARLEQUIN-COMMON-LISP> #<PACKAGE CAPI>)

Using :local-nicknames (note the warning because defining a local nickname that is the same as the global name of a different package is risky):

(defpackage "BAR" (:intern "X"))
(defpackage "FOO" (:intern "X"))
(defpackage "QUUX" (:local-nicknames ("BAR" "FOO") ("FOO" "BAR")))
Warning: Local nickname "BAR" for "FOO" in package "QUUX" matches name of "BAR"
Warning: Local nickname "FOO" for "BAR" in package "QUUX" matches name of "FOO"
 
(find-symbol "X" "FOO")
=>
FOO::X
 
(find-symbol "X" "BAR")
=>
BAR::X
 
(let ((*package* (find-package "QUUX")))
  (find-symbol "X" "FOO"))
=> BAR::X
 
(let ((*package* (find-package "QUUX")))
  (find-symbol "X" "BAR"))
=> FOO::X
See also

defpackage in the Common Lisp HyperSpec
*handle-existing-defpackage*


LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:30