export
export Function
Syntax:
export symbols &optional package → t
Arguments and Values:
symbols—a designator for a list of symbols.
package—a package designator . The default is the current package.
Description:
export makes one or more symbols that are accessible in package (whether directly or by inheritance) be external symbols of that package.
If any of the symbols is already accessible as an external symbol of package, export has no effect on that symbol. If the symbol is present in package as an internal symbol, it is simply changed to external status. If it is accessible as an internal symbol via use-package, it is first imported into package, then exported. (The symbol is then present in the package whether or not package continues to use the package through which the symbol was originally inherited.)
export makes each symbol accessible to all the packages that use package. All of these packages are checked for name conflicts: (export s p) does (find-symbol (symbol-name s) q) for each package q in (package-used-by-list p). Note that in the usual case of an export during the initial definition of a package, the result of package-used-by-list is nil and the name-conflict checking takes negligible time. When multiple changes are to be made, for example when export is given a list of symbols, it is permissible for the implementation to process each change separately, so that aborting from a name conflict caused by any but the first symbol in the list does not unexport the first symbol in the list. However, aborting from a name-conflict error caused by export of one of symbols does not leave that symbol accessible to some packages and inaccessible to others; with respect to each of symbols processed, export behaves as if it were as an atomic operation.
A name conflict in export between one of symbols being exported and a symbol already present in a package that would inherit the newly-exported symbol may be resolved in favor of the exported symbol by uninterning the other one, or in favor of the already-present symbol by making it a shadowing symbol.
Examples:
(make-package ’temp :use nil) → #<PACKAGE "TEMP">
(use-package ’temp) → T
(intern "TEMP-SYM" ’temp) → TEMP::TEMP-SYM, NIL
(find-symbol "TEMP-SYM") → NIL, NIL
(export (find-symbol "TEMP-SYM" ’temp) ’temp) → T
(find-symbol "TEMP-SYM") → TEMP-SYM, :INHERITED
Side Effects:
The package system is modified.
Affected By:
Accessible symbols.
Exceptional Situations:
If any of the symbols is not accessible at all in package, an error of type package-error is signaled that is correctable by permitting the user to interactively specify whether that symbol should be imported.
See Also:
import, unexport, Section 11.1 (Package Concepts)
Expanded Reference: export
Basic Usage: Exporting a Symbol from a Package
export makes symbols that are accessible in a package become external symbols of that package, allowing other packages that use it to inherit them.
;; Create a package and intern a symbol
(defpackage "MY-LIB" (:use))
(intern "GREET" "MY-LIB")
;; The symbol is internal by default
(find-symbol "GREET" "MY-LIB")
;; => MY-LIB::GREET
;; => :INTERNAL
;; Export it
(export (find-symbol "GREET" "MY-LIB") "MY-LIB")
;; => T
;; Now it is external
(find-symbol "GREET" "MY-LIB")
;; => MY-LIB:GREET
;; => :EXTERNAL
Exporting Makes Symbols Inherited by Using Packages
When a symbol is exported, it becomes visible to all packages that use the exporting package.
(defpackage "MATH-UTILS" (:use))
(defpackage "APP" (:use "MATH-UTILS"))
(intern "ADD" "MATH-UTILS")
(find-symbol "ADD" "APP")
=> NIL
=> NIL
(export (find-symbol "ADD" "MATH-UTILS") "MATH-UTILS")
=> T
(find-symbol "ADD" "APP")
;; => MATH-UTILS:ADD
;; => :INHERITED
Exporting Multiple Symbols at Once
export accepts a list of symbols.
(defpackage "TOOLS" (:use))
(intern "HAMMER" "TOOLS")
(intern "SAW" "TOOLS")
(intern "DRILL" "TOOLS")
(export (list (find-symbol "HAMMER" "TOOLS")
(find-symbol "SAW" "TOOLS"))
"TOOLS")
=> T
(find-symbol "HAMMER" "TOOLS")
;; => TOOLS:HAMMER
;; => :EXTERNAL
(find-symbol "SAW" "TOOLS")
;; => TOOLS:SAW
;; => :EXTERNAL
(find-symbol "DRILL" "TOOLS")
;; => TOOLS::DRILL
;; => :INTERNAL
Exporting an Already-External Symbol is a No-Op
(defpackage "ALREADY-EXT" (:use) (:export "FOO"))
(find-symbol "FOO" "ALREADY-EXT")
;; => ALREADY-EXT:FOO
;; => :EXTERNAL
;; Exporting again has no effect and returns T
(export (find-symbol "FOO" "ALREADY-EXT") "ALREADY-EXT")
;; => T
Using export with the Current Package
When the optional package argument is omitted, export operates on the current package.
(defpackage "WORK" (:use "COMMON-LISP"))
(in-package "WORK")
(defun helper () "I help")
(export 'helper)
;; => T
(in-package "COMMON-LISP-USER")
(find-symbol "HELPER" "WORK")
;; => WORK:HELPER
;; => :EXTERNAL