Why are uninterned symbols used for package names and exports in Common Lisp?

后端 未结 2 877
故里飘歌
故里飘歌 2021-02-07 09:27

In a screen cast on Common List the author uses uninterned symbols for package names and exports.

(defpackage #:foo
  (:use :cl)
  (:export #:bar
           #:ba         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 09:54

    Using an interned symbol pollutes the package you're currently in with symbols that are only used for their names anyway:

    [1]> *package*
    #
    [2]> (defpackage bar)
    #
    [3]> (find-symbol "BAR")
    BAR ;
    :INTERNAL
    

    Uninterned symbols don't do that:

    ;; Uninterned symbols don't cause symbol pollution:
    [4]> (defpackage #:foo)
    #
    [5]> (find-symbol "FOO")
    NIL ;
    NIL
    

    You can also use strings directly, but since you're usually dealing with uppercase symbol names, they are less convenient to write:

    [6]> (defpackage "BARFOO")
    #
    [7]> (find-symbol "BARFOO")
    NIL ;
    NIL
    

    Example

    To illustrate the problem, consider the following interaction:

    [1]> (defpackage hello (:use cl) (:export hello))
    #
    
    ;; Let's write some FOO stuff...
    [2]> (defpackage foo (:use cl))
    #
    [3]> (in-package foo)
    #
    
    ;; Oh, I forgot to import HELLO!
    ;; Let's fix that.
    FOO[4]> (defpackage foo (:use cl hello))
    *** - (COMMON-LISP:USE-PACKAGE (# #)
          #): 1 name conflicts remain
          Which symbol with name "HELLO" should be accessible in #?
    
    ;; Oops.
    

提交回复
热议问题