The less-familiar parts of Lisp for beginners — shadowing-import

Now, we discuss shadowing-import.  We skipped over import earlier, as it seemed fairly simple, but shadowing-import is interesting in the way it differs from import, so we’ll talk about it a bit now.

The import function allows a symbol from one package to be visible in another.  Note, however, that if a symbol by that name already exists in the package into which the symbol is being imported, an error condition is raised.  To avoid the error, you first have to unintern the existing symbol in the package before importing the symbol from the other package.

The shadowing-import function works like import, but shadows any existing symbol rather than raising an error condition.  The imported symbol replaces the formerly-existing one.

Here is a transcript of that behaviour.  Note that import fails with an error, while shadowing-import succeeds:
*slime-repl sbcl*

CL-USER> (make-package 'foreign)
#<PACKAGE "FOREIGN">
CL-USER> (intern "MY-SYMBOL" 'foreign)
FOREIGN::MY-SYMBOL
NIL
CL-USER> (intern "MY-SYMBOL")
MY-SYMBOL
NIL
CL-USER> (import 'foreign::MY-SYMBOL)
; Evaluation aborted on #<NAME-CONFLICT {1002FD4373}>.
CL-USER> (shadowing-import 'foreign::MY-SYMBOL)
T

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.