Makketronix
Makketronix

Reputation: 1460

Common Lisp appending package name to quoted keys in macros

I noticed the following behavior in Common Lisp (at least using SBCL), which I was able to reduce it to the following:

Suppose I have the following Macro:

(defpackage "MY-TEST"
  (:use "COMMON-LISP")
  (:export :appended
       :not-appended))

(in-package :MY-TEST)

(defmacro not-appended ()
  `(list ':type 'array))

(defmacro appended ()
  `(list ':type 'something-else))

The following is the output:

* (my-test:not-appended) 
(:TYPE ARRAY)


* (my-test:appended) 
(:TYPE MY-TEST::SOMETHING-ELSE)

Notice that in the second macro, the namespace is preceding the "SOMETHING-ELSE".

Questions:

Upvotes: 2

Views: 140

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139251

Note that this is fully unrelated to macros and is an effect of packages, symbols and how symbols are printed:

The package `MY-TEST':

CL-USER 2 > (defpackage "MY-TEST"
              (:use "COMMON-LISP")
              (:export :appended
               :not-appended))
#<The MY-TEST package, 0/16 internal, 2/16 external>

Making the package the current package by calling in-package:

CL-USER 3 > (in-package :MY-TEST)
#<The MY-TEST package, 0/16 internal, 2/16 external>

Let's compute a list of the symbols array and foo. See how the REPL prints it as (ARRAY FOO) because both symbols are accessible in package MY-TEST.

MY-TEST 4 > (list 'array 'foo)
(ARRAY FOO)

Making CL-USER the current package:

MY-TEST 5 > (in-package :cl-user)
#<The COMMON-LISP-USER package, 151/256 internal, 0/4 external>

Now let's get the second last value and see how the REPL prints it:

CL-USER 6 > **
(ARRAY MY-TEST::FOO)

ARRAY is printed without package prefix, because it is the same symbol from the package COMMON-LISP (which was used in the package MY-TEST). FOO is printed with the package prefix MY-TEST, because it is an internal symbol in that package - it was interned there, because then the current package was MY-TEST. There are two colons, because the symbol FOO is not exported from the package MY-TEST and it is also not imported into the package CL-USER.

The package "CL" and "CL-USER" contain all the symbols from the programming language Common Lisp - thus importing "CL" into your own package makes all of those symbols available in that package, too.

CL-USER 7 > (let ((l '()))
              (do-symbols (sym (find-package "CL") l)
                (pushnew sym l)))
(MAKE-ARRAY INVOKE-DEBUGGER STRING-TRIM WILD-PATHNAME-P UNREAD-CHAR RESTART-BIND ...

Upvotes: 7

Related Questions