Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Problem with packages and READ
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Thomas A. Russ  
View profile  
 More options Oct 21 2010, 12:26 pm
Newsgroups: comp.lang.lisp
From: t...@sevak.isi.edu (Thomas A. Russ)
Date: 21 Oct 2010 09:26:55 -0700
Local: Thurs, Oct 21 2010 12:26 pm
Subject: Re: Problem with packages and READ

Josef Wolf <j...@raven.inka.de> writes:
> Hello,

> I am somewhat struggling with the package system. Here is  an excerpt:

>   CL-USER> (in-package :cl-user)
>   #<PACKAGE "COMMON-LISP-USER">
>   CL-USER> (defpackage :foo (:use :cl) (:export :huhu))
>   #<PACKAGE "FOO">
>   CL-USER> (in-package :foo)
>   #<PACKAGE "FOO">
>   FOO> (defparameter *bar* 'bar)
>   *BAR*
>   FOO> (defun huhu ()
>     (list *bar* 'bar (read-from-string "bar")))
>   HUHU
>   FOO> (huhu)
>   (BAR BAR BAR)
>   FOO> (in-package :cl-user)
>   #<PACKAGE "COMMON-LISP-USER">
>   CL-USER> (foo:huhu)
>   (FOO::BAR FOO::BAR BAR)
>   CL-USER> (defpackage :baz)
>   #<PACKAGE "BAZ">
>   CL-USER> (in-package :baz)
>   #<COMMON-LISP:PACKAGE "BAZ">
>   BAZ> (foo:huhu)
>   (FOO::BAR FOO::BAR BAR)
>   BAZ>

> So READ seems to put symbols into the "toplevel" package, while DEFPARAMETER
> seems to honor the IN-PACKAGE setting.

Well, for starters, DEFPARAMETER doesn't really know anything about
packages and it doesn't care.  Packages affect the way the reader
converts strings into symbols.  So the real issue is what package is the
"current package" when the reader encounters a string that it needs to
turn into a symbol.

There are essentially two levels to this.  The first is an explicit
package designation, using : or :: to indicate the package in which a
symbol is supposed to be accessible.[1] That is used to find an
appropriately matching symbol (which may actually be in a different
package)[2] or to indicate in which package a new symbol is created.  If
there is no explicit package indicated, then the current value of
*package* is used

IN-PACKAGE is one way of setting the current package, which is kept in
the variable CL:*PACKAGE*.

> Is there any way to tell READ to put the symbols into the FOO package?

So, yes, there are a couple of ways to do this.  One of the simplest is
to just use a package prefix:

   (defun huhu ()
     (list *bar* 'bar (read-from-string "foo::bar")))

By the way, the reason *bar* and 'bar are in the FOO package is because
that is the current package when this form is READ by the reader before
being passed on to the evaluator (or compiler).  Remember that it is the
reading process that determines in which package symbols in the code end
up.[3]  And since there was an (in-package :foo) before this code was
read, the current package was set to FOO.

So, explicit qualification works if you have a constant string to read.
If you want to process an arbitrary string in a particular package, you
can bind *package* to the package you want:

   (defun huhu ()
     (let ((*package* (find-package "FOO")))
       (list *bar* 'bar (read-from-string "bar"))))

=========== Notes ===========

[1] There is also #: to indicate to tell the reader to not intern the
symbol in a package at all.

[2] The package qualifier indicates the package in which the lookup of
an existing symbol occurs.  So if there is an explicitly or implicitly
imported symbol that matches, it will be returned even if it is in a
different package.  In that way  cl-user::car and cl:car are the same
symbol.

[3] OK, there are program calls like INTERN that can also do this, but
we aren't using them in this example.

--
Thomas A. Russ,  USC/Information Sciences Institute


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.