📝Common Lisp: packages and symbols
symbols
- symbol without a colon is an unqualified name
- symbol with one colon can only refer to exported (public) symbols
- symbol with double-colon can refer to unexported symbols
- keyword symbols start with colon and are interned in
KEYWORD
package and automatically exported - uninterned symbols are written with a leading
#:
. Every time the reader reads a name starting with#:
, it creates a new symbol
- accessible: all the symbols that can be found in a given package with
find-symbol
(= symbols that can be referred to with unqualified names when the package is current) - present: symbol is contained in package’s name-to-symbol table
- The package in which a symbol is first interned (by the reader) is home package
- A package inherits symbols from other packages by using the other packages. Only external symbols are inherited. A symbol is made external by exporting (also makes it accessible via single-colon name).
- For each name, there can only be one symbol max. A symbol can be made shadowing which makes it shadow other symbols with the same name. Each package maintains a list of shadowing symbols.
- A symbol can be imported by adding it to the name-to-symbol table
A symbol can be uninterned from a package—removed from its name-to-symbol and shadowing tables
- a symbol that is not present in any package is called an uninterned symbol, and can no longer be read by the reader
- Package system in common lisp is only exporting symbols, not functions or variables.
;; define package
(defpackage :package.name
(:use :common-lisp :package2)
;; export symbols
(:export
:symbol1
:symbol2)
;; import symbols
(:import-from :other.package.name :symbol3 :symbol4)
;; shadow a symbol (e.g., from :use above)
;;
;; This creates a new 'symbol5 and adds it to package-to-name table
;; of package.name and to shadowing list
(:shadow :symbol5)
;; Shadowing import (makes symbol-name from other packages
;; inaccessible)
(:shadowing-import-from :package-name :symbol-name))