Subject: Re: same symbol name in 2 different packages
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 18 Jul 2006 04:33:17 -0500
Newsgroups: comp.lang.lisp
Message-ID: <Yeednf44uslANyHZnZ2dnUVZ_q6dnZ2d@speakeasy.net>
Kaz Kylheku <kkylheku@gmail.com> wrote:
+---------------
| Sacha wrote:
| > I need to use this make-parser in a web application in order to define a
| > grammar for url parsing. So i have this kind of declaration :
| > (defpackage :web-app-gui
| >     (:use :common-lisp :cl-who :tbnl :cl-drp :web-app-dal))
| 
| Note that USE-PACKAGE and the related :USE syntax in the DEFPACKAGE
| macro are a crude tool. When you USE a package, you are
| indiscriminately bringing in all of its exported symbols. So yes,
| clashes can happen, because people often think of the same symbols.
...
| Or, better yet, specify the precise list of symbols that you want to
| import from a package.
|  :import-from <package> <symbol> ...
|  :shadowing-import-from <package> <symbol> ...
| Typically, :use is just used for some base package like COMMON-LISP.
| You :use CL, and then from everything else you do a shadowing-import of
| the specific symbols that you want.
+---------------

But one can still do all that in DEFPACKAGE, which supports :SHADOW
and :SHADOWING-IMPORT-FROM and explicitly defines the order of those
with respect to :USE as being what you normally want:

    Macro DEFPACKAGE
    ...
    The order in which the options appear in a defpackage form is
    irrelevant. The order in which they are executed is as follows:

    1. :shadow and :shadowing-import-from.
    2. :use.
    3. :import-from and :intern.
    4. :export.

    Shadows are established first, since they might be necessary to
    block spurious name conflicts when the :use option is processed.
    The :use option is executed next so that :intern and :export options
    can refer to normally inherited symbols. The :export option is
    executed last so that it can refer to symbols created by any of
    the other options; in particular, shadowing symbols and imported
    symbols can be made external.

So assuming a conflict between, say, WEB-APP-GUI:FOO and
WEB-APP-DAL:FOO, the following DEFPACKAGE will reliably avoid
the conflict, giving preference to the package being defined:

    (defpackage :web-app-gui
      (:use :common-lisp :cl-who :tbnl :cl-drp :web-app-dal)
      (:shadow foo))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607