Re: package frenzy
- From: Peter Seibel <peter@xxxxxxxxxxxxxxx>
- Date: Mon, 30 May 2005 13:02:25 GMT
Joris Bleys <jorisb@xxxxxxxxxxxxxx> writes:
> On 2005-05-30 14:23:50 +0200, "Paul F. Dietz" <dietz@xxxxxxx> said:
>
>>> I'd like to use them to avoid nasty things like name-clashes
>>> (that's what they're for, right?).
>> No, you absolutely should use packages.
>
> Ok, so we tend to agree :).
>
>>
>>> Let's say I have some package :exporting-package, which has two
>>> functions: ep-internal-fn and ep-external-fn. The ep-internal-fn
>>> gets a string as argument and returns a symbol using (read-line str
>>> NIL NIL). The ep-external-fn uses eq to compare the result of
>>> ep-internal-fn with a pre-programmed symbol 'TEST. The
>>> ep-external-fn is exported.
>>> Now another package is defined :importing-package which uses the
>>> :exporting-package (use-package). When ep-external-fn is called,
>>> the equality is always false. This happens because the result of
>>> ep-external-fn is a symbol in the package :importing-package,
>>> whereas the 'TEST symbol is defined in the :exporting-package.
>> That symbol was part of the API of the exported function, so it
>> itself
>> should have been exported also. Or, you could have used a keyword
>> symbol.
>
> Maybe, I was a bit unclear with my problem statement. The symbol is
> not part of the API and should remain unvisible for the users of
> :exporting-package. So I guess what I want to do is to make sure that
> the result of the call to the read-line fn returns a symbol inside the
> :exported-package, but I don't know how to do so. Both ep-internal-fn
> and ep-external-fn are defined in the package :exporting-package
> (using in-package).
For starters, presumably you're using READ not READ-LINE as the latter
returns a string, not a symbol. Or you're using READ-LINE and then
INTERN to create a symbol, which is arguably a better plan that using
READ so you don't have to worry about READ reading some other kind of
object. At any rate, the problem you're having is that READ and INTERN
both use the runtime value of *PACKAGE* to determine how to
find/intern symbols. So it doesn't matter that ep-internal-fn happens
to be defined in a file that is read with a particular *PACKAGE*--what
matters is what *PACKAGE* is when it is called. However you can fix
this easily enough:
(defun ep-internal-fn (stream)
(let ((*package* (find-package :the-package-you-want)))
(read in nil nil)))
or
(defun ep-internal-fn (stream)
(let ((string (read-line in nil nil)))
(when string
(intern string :the-package-you-want))))
Note, in the later version you'll probably want to use (string-upcase
string) as the argument to INTERN in order to match the reader's
default upcasing of symbol names, i.e.
(defun ep-internal-fn (stream)
(let ((string (read-line in nil nil)))
(when string
(intern (string-upcase string) :the-package-you-want))))
-Peter
--
Peter Seibel peter@xxxxxxxxxxxxxxx
Lisp is the red pill. -- John Fraser, comp.lang.lisp
.
- Follow-Ups:
- Re: package frenzy
- From: Joris Bleys
- Re: package frenzy
- References:
- Re: package frenzy
- From: Paul F. Dietz
- Re: package frenzy
- From: Joris Bleys
- Re: package frenzy
- Prev by Date: Re: Macros and symbols across packages
- Next by Date: Re: package frenzy
- Previous by thread: Re: package frenzy
- Next by thread: Re: package frenzy
- Index(es):
Relevant Pages
|
|