From: Adrian DOZSA
Subject: in-package function vs macro
Date: 
Message-ID: <1178825793.911546.45740@q75g2000hsh.googlegroups.com>
Here's a problem i can't figure it out.
Let's say I want to do something like this:

> (let ((name "foo"))
      (in-package name)
      (.....))

The problem is that in-package is a macro and treats name as a string
and i end up with (in-package "name").

Trying to resolve the problem i ended up using the in-package
function:

> (let ((name "foo"))
      (funcall #'in-package name)
      (.....))
But this throws a strange error (on Lispworks):
Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).

Does anybody know what's wrong here? Or perhaps an other way to solve
what I'm trying to do.
Thanks.

From: Thibault Langlois
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178828670.359053.239030@p77g2000hsh.googlegroups.com>
On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> Here's a problem i can't figure it out.
> Let's say I want to do something like this:
>
> > (let ((name "foo"))
>
>       (in-package name)
>       (.....))
>
> The problem is that in-package is a macro and treats name as a string
> and i end up with (in-package "name").
>
> Trying to resolve the problem i ended up using the in-package
> function:
>
> > (let ((name "foo"))
>
>       (funcall #'in-package name)
>       (.....))
> But this throws a strange error (on Lispworks):
> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> Does anybody know what's wrong here? Or perhaps an other way to solve
> what I'm trying to do.
> Thanks.


Maybe you want to do:

(with-package 'foo
  (........))

(defmacro with-package (package-symbol &body body)
  (let ((last-package (gensym)))
    `(let ((,last-package *package*))
       (setq *package* (find-package ,package-symbol))
       (prog1
           (progn ,@body)
         (setq *package* ,last-package)))))

Thibault
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178829150.135849.221360@o5g2000hsb.googlegroups.com>
On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
wrote:
> On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>
>
>
> > Here's a problem i can't figure it out.
> > Let's say I want to do something like this:
>
> > > (let ((name "foo"))
>
> >       (in-package name)
> >       (.....))
>
> > The problem is that in-package is a macro and treats name as a string
> > and i end up with (in-package "name").
>
> > Trying to resolve the problem i ended up using the in-package
> > function:
>
> > > (let ((name "foo"))
>
> >       (funcall #'in-package name)
> >       (.....))
> > But this throws a strange error (on Lispworks):
> > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> > Does anybody know what's wrong here? Or perhaps an other way to solve
> > what I'm trying to do.
> > Thanks.
>
> Maybe you want to do:
>
> (with-package 'foo
>   (........))
>
> (defmacro with-package (package-symbol &body body)
>   (let ((last-package (gensym)))
>     `(let ((,last-package *package*))
>        (setq *package* (find-package ,package-symbol))
>        (prog1
>            (progn ,@body)
>          (setq *package* ,last-package)))))
>
> Thibault

This is exactly what I was trying to do. But I tried using in-package,
instead of using *package* directly.
From: Tim Bradshaw
Subject: Re: in-package function vs macro
Date: 
Message-ID: <f201mn$ldb$1$8302bc10@news.demon.co.uk>
On 2007-05-10 21:32:30 +0100, Adrian DOZSA <·········@gmail.com> said:

> This is exactly what I was trying to do. But I tried using in-package,
> instead of using *package* directly.

In case it's not clear this doesn't work because IN-PACKAGE is a macro. 
 The reason for *that* is because IN-PACKAGE must do things at compile 
time.  Originally IN-PACKAGE was a function, but then there was a 
requirement for the compiler to magically notice it.  Now it's a macro 
which should expand to a suitable EVAL-WHEN form, causing things to 
happen (specifically causing *PACKAGE* to be assigned to) at 
compile-time as well as other times.

--tim
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178832640.609686.257470@n59g2000hsh.googlegroups.com>
On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
wrote:
> On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>
>
>
> > Here's a problem i can't figure it out.
> > Let's say I want to do something like this:
>
> > > (let ((name "foo"))
>
> >       (in-package name)
> >       (.....))
>
> > The problem is that in-package is a macro and treats name as a string
> > and i end up with (in-package "name").
>
> > Trying to resolve the problem i ended up using the in-package
> > function:
>
> > > (let ((name "foo"))
>
> >       (funcall #'in-package name)
> >       (.....))
> > But this throws a strange error (on Lispworks):
> > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> > Does anybody know what's wrong here? Or perhaps an other way to solve
> > what I'm trying to do.
> > Thanks.
>
> Maybe you want to do:
>
> (with-package 'foo
>   (........))
>
> (defmacro with-package (package-symbol &body body)
>   (let ((last-package (gensym)))
>     `(let ((,last-package *package*))
>        (setq *package* (find-package ,package-symbol))
>        (prog1
>            (progn ,@body)
>          (setq *package* ,last-package)))))
>
> Thibault

Unfortunately your solution doesn't work, because at the end when you
try to set back the last-package you are in the new package and the
variable last-package was defined in the last-package. This is a
tricky one.
From: Richard M Kreuter
Subject: Re: in-package function vs macro
Date: 
Message-ID: <87ejlo73g2.fsf@tan-ru.localdomain>
Adrian DOZSA <·········@gmail.com> writes:
> On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
> wrote:
>> On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>> >
>> > Here's a problem i can't figure it out.
>> > Let's say I want to do something like this:
>>
>> > > (let ((name "foo"))
>>
>> >       (in-package name)
>> >       (.....))
>>
>> Maybe you want to do:
>>
>> (with-package 'foo
>>   (........))
>>
>> (defmacro with-package (package-symbol &body body)
>>   (let ((last-package (gensym)))
>>     `(let ((,last-package *package*))
>>        (setq *package* (find-package ,package-symbol))
>>        (prog1
>>            (progn ,@body)
>>          (setq *package* ,last-package)))))
>>
>
> Unfortunately your solution doesn't work, because at the end when
> you try to set back the last-package you are in the new package and
> the variable last-package was defined in the last-package. This is a
> tricky one.

Not really, you can rebind *PACKAGE* with LET.

But what are you trying to do in the code where *PACKAGE* is rebound?

--
RmK
From: Rainer Joswig
Subject: Re: in-package function vs macro
Date: 
Message-ID: <joswig-E17E6D.23400310052007@news-europe.giganews.com>
In article <························@n59g2000hsh.googlegroups.com>,
 Adrian DOZSA <·········@gmail.com> wrote:

> On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
> wrote:
> > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> >
> >
> >
> > > Here's a problem i can't figure it out.
> > > Let's say I want to do something like this:
> >
> > > > (let ((name "foo"))
> >
> > >       (in-package name)
> > >       (.....))
> >
> > > The problem is that in-package is a macro and treats name as a string
> > > and i end up with (in-package "name").
> >
> > > Trying to resolve the problem i ended up using the in-package
> > > function:
> >
> > > > (let ((name "foo"))
> >
> > >       (funcall #'in-package name)
> > >       (.....))
> > > But this throws a strange error (on Lispworks):
> > > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
> >
> > > Does anybody know what's wrong here? Or perhaps an other way to solve
> > > what I'm trying to do.
> > > Thanks.
> >
> > Maybe you want to do:
> >
> > (with-package 'foo
> >   (........))
> >
> > (defmacro with-package (package-symbol &body body)
> >   (let ((last-package (gensym)))
> >     `(let ((,last-package *package*))
> >        (setq *package* (find-package ,package-symbol))
> >        (prog1
> >            (progn ,@body)
> >          (setq *package* ,last-package)))))
> >
> > Thibault
> 
> Unfortunately your solution doesn't work, because at the end when you
> try to set back the last-package you are in the new package and the
> variable last-package was defined in the last-package. This is a
> tricky one.

Have you tried it?

Can you imagine why saying 'you are in the new package'
makes no sense here?

-- 
http://lispm.dyndns.org
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178833845.645034.106700@w5g2000hsg.googlegroups.com>
On May 11, 12:40 am, Rainer Joswig <······@lisp.de> wrote:
> In article <························@n59g2000hsh.googlegroups.com>,
>  Adrian DOZSA <·········@gmail.com> wrote:
>
>
>
>
>
>
>
> > On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
> > wrote:
> > > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>
> > > > Here's a problem i can't figure it out.
> > > > Let's say I want to do something like this:
>
> > > > > (let ((name "foo"))
>
> > > >       (in-package name)
> > > >       (.....))
>
> > > > The problem is that in-package is a macro and treats name as a string
> > > > and i end up with (in-package "name").
>
> > > > Trying to resolve the problem i ended up using the in-package
> > > > function:
>
> > > > > (let ((name "foo"))
>
> > > >       (funcall #'in-package name)
> > > >       (.....))
> > > > But this throws a strange error (on Lispworks):
> > > > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > > > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> > > > Does anybody know what's wrong here? Or perhaps an other way to solve
> > > > what I'm trying to do.
> > > > Thanks.
>
> > > Maybe you want to do:
>
> > > (with-package 'foo
> > >   (........))
>
> > > (defmacro with-package (package-symbol &body body)
> > >   (let ((last-package (gensym)))
> > >     `(let ((,last-package *package*))
> > >        (setq *package* (find-package ,package-symbol))
> > >        (prog1
> > >            (progn ,@body)
> > >          (setq *package* ,last-package)))))
>
> > > Thibault
>
> > Unfortunately your solution doesn't work, because at the end when you
> > try to set back the last-package you are in the new package and the
> > variable last-package was defined in the last-package. This is a
> > tricky one.
>
> Have you tried it?
>
> Can you imagine why saying 'you are in the new package'
> makes no sense here?
>
> --http://lispm.dyndns.org

Sorry, my mistake. My repl was full of tests and i've misunderstood my
tests. It does work.
Thanks.
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178834790.450048.151420@y80g2000hsf.googlegroups.com>
On May 11, 12:50 am, Adrian DOZSA <·········@gmail.com> wrote:
> On May 11, 12:40 am, Rainer Joswig <······@lisp.de> wrote:
>
>
>
> > In article <························@n59g2000hsh.googlegroups.com>,
> >  Adrian DOZSA <·········@gmail.com> wrote:
>
> > > On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
> > > wrote:
> > > > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>
> > > > > Here's a problem i can't figure it out.
> > > > > Let's say I want to do something like this:
>
> > > > > > (let ((name "foo"))
>
> > > > >       (in-package name)
> > > > >       (.....))
>
> > > > > The problem is that in-package is a macro and treats name as a string
> > > > > and i end up with (in-package "name").
>
> > > > > Trying to resolve the problem i ended up using the in-package
> > > > > function:
>
> > > > > > (let ((name "foo"))
>
> > > > >       (funcall #'in-package name)
> > > > >       (.....))
> > > > > But this throws a strange error (on Lispworks):
> > > > > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > > > > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> > > > > Does anybody know what's wrong here? Or perhaps an other way to solve
> > > > > what I'm trying to do.
> > > > > Thanks.
>
> > > > Maybe you want to do:
>
> > > > (with-package 'foo
> > > >   (........))
>
> > > > (defmacro with-package (package-symbol &body body)
> > > >   (let ((last-package (gensym)))
> > > >     `(let ((,last-package *package*))
> > > >        (setq *package* (find-package ,package-symbol))
> > > >        (prog1
> > > >            (progn ,@body)
> > > >          (setq *package* ,last-package)))))
>
> > > > Thibault
>
> > > Unfortunately your solution doesn't work, because at the end when you
> > > try to set back the last-package you are in the new package and the
> > > variable last-package was defined in the last-package. This is a
> > > tricky one.
>
> > Have you tried it?
>
> > Can you imagine why saying 'you are in the new package'
> > makes no sense here?
>
> > --http://lispm.dyndns.org
>
> Sorry, my mistake. My repl was full of tests and i've misunderstood my
> tests. It does work.
> Thanks.

I might seem crazy but it really doesn't work. Here a little test:
(clean repl)

CL-USER 1 > (defpackage foo)
#<PACKAGE FOO>

CL-USER 2 > (in-package :foo)
#<PACKAGE FOO>

FOO 3 > (defun bar ()
                  123)
BAR

FOO 4 > (in-package :cl-user)
#<PACKAGE COMMON-LISP-USER>

CL-USER 5 > (defmacro with-package (package-symbol &body body)
              (let ((last-package (gensym)))
                `(let ((,last-package *package*))
                   (setq *package* (find-package ,package-symbol))
                   (prog1
                       (progn ,@body)
                     (setq *package* ,last-package)))))
WITH-PACKAGE

CL-USER 6 > (with-package :foo
                          (format t "~A" (package-name *package*))
                          (bar))
FOO
Error: Undefined function COMMON-LISP-USER::BAR called with arguments
().

The strangest thing is that format prints the *package* as being FOO
but when you try to call BAR within the package it says "Undefined
function COMMON-LISP-USER::BAR", but *package* is FOO. Could someone
explain this to me please.
From: Rainer Joswig
Subject: Re: in-package function vs macro
Date: 
Message-ID: <joswig-B5B982.00330811052007@news-europe.giganews.com>
In article <························@y80g2000hsf.googlegroups.com>,
 Adrian DOZSA <·········@gmail.com> wrote:

> On May 11, 12:50 am, Adrian DOZSA <·········@gmail.com> wrote:
> > On May 11, 12:40 am, Rainer Joswig <······@lisp.de> wrote:
> >
> >
> >
> > > In article <························@n59g2000hsh.googlegroups.com>,
> > >  Adrian DOZSA <·········@gmail.com> wrote:
> >
> > > > On May 10, 11:24 pm, Thibault Langlois <·················@gmail.com>
> > > > wrote:
> > > > > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> >
> > > > > > Here's a problem i can't figure it out.
> > > > > > Let's say I want to do something like this:
> >
> > > > > > > (let ((name "foo"))
> >
> > > > > >       (in-package name)
> > > > > >       (.....))
> >
> > > > > > The problem is that in-package is a macro and treats name as a string
> > > > > > and i end up with (in-package "name").
> >
> > > > > > Trying to resolve the problem i ended up using the in-package
> > > > > > function:
> >
> > > > > > > (let ((name "foo"))
> >
> > > > > >       (funcall #'in-package name)
> > > > > >       (.....))
> > > > > > But this throws a strange error (on Lispworks):
> > > > > > Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > > > > > definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
> >
> > > > > > Does anybody know what's wrong here? Or perhaps an other way to solve
> > > > > > what I'm trying to do.
> > > > > > Thanks.
> >
> > > > > Maybe you want to do:
> >
> > > > > (with-package 'foo
> > > > >   (........))
> >
> > > > > (defmacro with-package (package-symbol &body body)
> > > > >   (let ((last-package (gensym)))
> > > > >     `(let ((,last-package *package*))
> > > > >        (setq *package* (find-package ,package-symbol))
> > > > >        (prog1
> > > > >            (progn ,@body)
> > > > >          (setq *package* ,last-package)))))
> >
> > > > > Thibault
> >
> > > > Unfortunately your solution doesn't work, because at the end when you
> > > > try to set back the last-package you are in the new package and the
> > > > variable last-package was defined in the last-package. This is a
> > > > tricky one.
> >
> > > Have you tried it?
> >
> > > Can you imagine why saying 'you are in the new package'
> > > makes no sense here?
> >
> > > --http://lispm.dyndns.org
> >
> > Sorry, my mistake. My repl was full of tests and i've misunderstood my
> > tests. It does work.
> > Thanks.
> 
> I might seem crazy but it really doesn't work. Here a little test:
> (clean repl)
> 
> CL-USER 1 > (defpackage foo)
> #<PACKAGE FOO>
> 
> CL-USER 2 > (in-package :foo)
> #<PACKAGE FOO>
> 
> FOO 3 > (defun bar ()
>                   123)
> BAR
> 
> FOO 4 > (in-package :cl-user)
> #<PACKAGE COMMON-LISP-USER>
> 
> CL-USER 5 > (defmacro with-package (package-symbol &body body)
>               (let ((last-package (gensym)))
>                 `(let ((,last-package *package*))
>                    (setq *package* (find-package ,package-symbol))
>                    (prog1
>                        (progn ,@body)
>                      (setq *package* ,last-package)))))
> WITH-PACKAGE
> 
> CL-USER 6 > (with-package :foo
>                           (format t "~A" (package-name *package*))
>                           (bar))
> FOO
> Error: Undefined function COMMON-LISP-USER::BAR called with arguments
> ().
> 
> The strangest thing is that format prints the *package* as being FOO
> but when you try to call BAR within the package it says "Undefined
> function COMMON-LISP-USER::BAR", but *package* is FOO. Could someone
> explain this to me please.

I think my other message has explained why it works, but not
the way you want it to.

-- 
http://lispm.dyndns.org
From: Pascal Bourguignon
Subject: Re: in-package function vs macro
Date: 
Message-ID: <876470b4tb.fsf@thalassa.lan.informatimago.com>
Adrian DOZSA <·········@gmail.com> writes:

> The strangest thing is that format prints the *package* as being FOO
> but when you try to call BAR within the package it says "Undefined
> function COMMON-LISP-USER::BAR", but *package* is FOO. Could someone
> explain this to me please.


Perhaps now is the time to read this nice tutorial:

"The Complete Idiot's Guide to Common Lisp Packages"
 http://www.flownet.com/gat/packages.pdf

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Small brave carnivores
Kill pine cones and mosquitoes
Fear vacuum cleaner
From: Tim Bradshaw
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178879688.125966.27030@y80g2000hsf.googlegroups.com>
On May 10, 11:06 pm, Adrian DOZSA <·········@gmail.com> wrote:

> The strangest thing is that format prints the *package* as being FOO
> but when you try to call BAR within the package it says "Undefined
> function COMMON-LISP-USER::BAR", but *package* is FOO. Could someone
> explain this to me please.

Think about when things happen and what the package is.  When is this
form read?  What is the package then?
From: Dan Bensen
Subject: Re: in-package function vs macro
Date: 
Message-ID: <f21ne9$2f0$1@wildfire.prairienet.org>
Adrian DOZSA wrote:
> CL-USER 6 > (with-package :foo
>                           (format t "~A" (package-name *package*))
>                           (bar))
> Error: Undefined function COMMON-LISP-USER::BAR
> The strangest thing is that format prints the *package* as being FOO
> but when you try to call BAR within the package it says "Undefined
> function COMMON-LISP-USER::BAR", but *package* is FOO. Could someone
> explain this to me please.

Interning of symbols occurs at read time, so even a macro is too late.
You have to change the package before READ reads that section of your
program.

-- 
Dan
www.prairienet.org/~dsb/
From: Pascal Bourguignon
Subject: Re: in-package function vs macro
Date: 
Message-ID: <87y7jwju41.fsf@thalassa.lan.informatimago.com>
Thibault Langlois <·················@gmail.com> writes:

> On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
>> Here's a problem i can't figure it out.
>> Let's say I want to do something like this:
>>
>> > (let ((name "foo"))
>>
>>       (in-package name)
>>       (.....))
>>
>> The problem is that in-package is a macro and treats name as a string
>> and i end up with (in-package "name").
>>
>> Trying to resolve the problem i ended up using the in-package
>> function:
>>
>> > (let ((name "foo"))
>>
>>       (funcall #'in-package name)
>>       (.....))
>> But this throws a strange error (on Lispworks):
>> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
>> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>>
>> Does anybody know what's wrong here? Or perhaps an other way to solve
>> what I'm trying to do.
>> Thanks.
>
>
> Maybe you want to do:
>
> (with-package 'foo
>   (........))
>
> (defmacro with-package (package-symbol &body body)
>   (let ((last-package (gensym)))
>     `(let ((,last-package *package*))
>        (setq *package* (find-package ,package-symbol))
>        (prog1
>            (progn ,@body)
>          (setq *package* ,last-package)))))

Why so much hate?

- use multiple-value-prog1 instead of prog1 that lose all the other values.
- use unwind-protect to restore *package* to last-package.
- don't use any of the above, just let:


(defmacro with-package (package-symbol &body body)
   `(let ((*package* (find-package ,package-symbol)))
        ,@body))


But what's the point:

(with-package  'foo
   (list (read) (read)))

vs.:

(let ((*package* (find-package 'foo)))
    (list (read) (read)))

?



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

The world will now reboot.  don't bother saving your artefacts.
From: Thibault Langlois
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178832225.095409.16800@e65g2000hsc.googlegroups.com>
On May 10, 9:34 pm, Pascal Bourguignon <····@informatimago.com> wrote:
> Thibault Langlois <·················@gmail.com> writes:
> > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> >> Here's a problem i can't figure it out.
> >> Let's say I want to do something like this:
>
> >> > (let ((name "foo"))
>
> >>       (in-package name)
> >>       (.....))
>
> >> The problem is that in-package is a macro and treats name as a string
> >> and i end up with (in-package "name").
>
> >> Trying to resolve the problem i ended up using the in-package
> >> function:
>
> >> > (let ((name "foo"))
>
> >>       (funcall #'in-package name)
> >>       (.....))
> >> But this throws a strange error (on Lispworks):
> >> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> >> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> >> Does anybody know what's wrong here? Or perhaps an other way to solve
> >> what I'm trying to do.
> >> Thanks.
>
> > Maybe you want to do:
>
> > (with-package 'foo
> >   (........))
>
> > (defmacro with-package (package-symbol &body body)
> >   (let ((last-package (gensym)))
> >     `(let ((,last-package *package*))
> >        (setq *package* (find-package ,package-symbol))
> >        (prog1
> >            (progn ,@body)
> >          (setq *package* ,last-package)))))
>
> Why so much hate?
>
> - use multiple-value-prog1 instead of prog1 that lose all the other values.
> - use unwind-protect to restore *package* to last-package.
> - don't use any of the above, just let:
>
> (defmacro with-package (package-symbol &body body)
>    `(let ((*package* (find-package ,package-symbol)))
>         ,@body))
>

I always hesitate to post code on c.l.l because although I use cl a
lot I do not consider myself as a skilled cl programmer (I'm still
learning).
Anyway, thanks for the correction.

Thibault

> But what's the point:
>
> (with-package  'foo
>    (list (read) (read)))
>
> vs.:
>
> (let ((*package* (find-package 'foo)))
>     (list (read) (read)))
>
> ?
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> The world will now reboot.  don't bother saving your artefacts.
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178833170.250241.26900@h2g2000hsg.googlegroups.com>
On May 10, 11:34 pm, Pascal Bourguignon <····@informatimago.com>
wrote:
> Thibault Langlois <·················@gmail.com> writes:
> > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> >> Here's a problem i can't figure it out.
> >> Let's say I want to do something like this:
>
> >> > (let ((name "foo"))
>
> >>       (in-package name)
> >>       (.....))
>
> >> The problem is that in-package is a macro and treats name as a string
> >> and i end up with (in-package "name").
>
> >> Trying to resolve the problem i ended up using the in-package
> >> function:
>
> >> > (let ((name "foo"))
>
> >>       (funcall #'in-package name)
> >>       (.....))
> >> But this throws a strange error (on Lispworks):
> >> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> >> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> >> Does anybody know what's wrong here? Or perhaps an other way to solve
> >> what I'm trying to do.
> >> Thanks.
>
> > Maybe you want to do:
>
> > (with-package 'foo
> >   (........))
>
> > (defmacro with-package (package-symbol &body body)
> >   (let ((last-package (gensym)))
> >     `(let ((,last-package *package*))
> >        (setq *package* (find-package ,package-symbol))
> >        (prog1
> >            (progn ,@body)
> >          (setq *package* ,last-package)))))
>
> Why so much hate?
>
> - use multiple-value-prog1 instead of prog1 that lose all the other values.
> - use unwind-protect to restore *package* to last-package.
> - don't use any of the above, just let:
>
> (defmacro with-package (package-symbol &body body)
>    `(let ((*package* (find-package ,package-symbol)))
>         ,@body))
>
> But what's the point:
>
> (with-package  'foo
>    (list (read) (read)))
>
> vs.:
>
> (let ((*package* (find-package 'foo)))
>     (list (read) (read)))
>
> ?
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> The world will now reboot.  don't bother saving your artefacts.

Your solution is simple and clean. But unfortunately it doesn't work.

CL-USER 82 > (defpackage "foo")
#<PACKAGE foo>

CL-USER 83 > (in-package :foo)
#<PACKAGE FOO>

FOO 84 > (defun bar ())
BAR

FOO 85 > (in-package :cl-user)
#<PACKAGE COMMON-LISP-USER>

CL-USER 86 > (let ((*package* (find-package 'foo)))
                         (bar))

Error: Undefined function COMMON-LISP-USER::BAR called with arguments
().

The problem is that in the body of LET for some reasons the local
variable *package* doesn't behave like the global variable.
From: John Thingstad
Subject: Re: in-package function vs macro
Date: 
Message-ID: <op.tr4r4y1apqzri1@pandora.upc.no>
On Thu, 10 May 2007 23:39:30 +0200, Adrian DOZSA <·········@gmail.com>  
wrote:

>
> The problem is that in the body of LET for some reasons the local
> variable *package* doesn't behave like the global variable.
>

Of cource not. It has lexical scope.
If you want dynamic scope as with global variables:
(declare (special *package*))
Now *package* stays assigned to (find-package..) until it goes out of  
scope and
all functions called in that scope will see it too.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Rainer Joswig
Subject: Re: in-package function vs macro
Date: 
Message-ID: <joswig-735E7B.00032511052007@news-europe.giganews.com>
In article <·················@pandora.upc.no>,
 "John Thingstad" <··············@chello.no> wrote:

> On Thu, 10 May 2007 23:39:30 +0200, Adrian DOZSA <·········@gmail.com>  
> wrote:
> 
> >
> > The problem is that in the body of LET for some reasons the local
> > variable *package* doesn't behave like the global variable.
> >
> 
> Of cource not. It has lexical scope.

Wrong.

> If you want dynamic scope as with global variables:
> (declare (special *package*))
> Now *package* stays assigned to (find-package..) until it goes out of  
> scope and
> all functions called in that scope will see it too.

No that's wrong.

(let ((*package* (find-package :foo)))
  ...)


*package* has dynamic scope. It is declared as a special
variable. That declaration is also valid for local
rebindings. No need to declare it SPECIAL in
a local LET.

-- 
http://lispm.dyndns.org
From: Rainer Joswig
Subject: Re: in-package function vs macro
Date: 
Message-ID: <joswig-80C8CF.23594810052007@news-europe.giganews.com>
In article <·······················@h2g2000hsg.googlegroups.com>,
 Adrian DOZSA <·········@gmail.com> wrote:

> On May 10, 11:34 pm, Pascal Bourguignon <····@informatimago.com>
> wrote:
> > Thibault Langlois <·················@gmail.com> writes:
> > > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> > >> Here's a problem i can't figure it out.
> > >> Let's say I want to do something like this:
> >
> > >> > (let ((name "foo"))
> >
> > >>       (in-package name)
> > >>       (.....))
> >
> > >> The problem is that in-package is a macro and treats name as a string
> > >> and i end up with (in-package "name").
> >
> > >> Trying to resolve the problem i ended up using the in-package
> > >> function:
> >
> > >> > (let ((name "foo"))
> >
> > >>       (funcall #'in-package name)
> > >>       (.....))
> > >> But this throws a strange error (on Lispworks):
> > >> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > >> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
> >
> > >> Does anybody know what's wrong here? Or perhaps an other way to solve
> > >> what I'm trying to do.
> > >> Thanks.
> >
> > > Maybe you want to do:
> >
> > > (with-package 'foo
> > >   (........))
> >
> > > (defmacro with-package (package-symbol &body body)
> > >   (let ((last-package (gensym)))
> > >     `(let ((,last-package *package*))
> > >        (setq *package* (find-package ,package-symbol))
> > >        (prog1
> > >            (progn ,@body)
> > >          (setq *package* ,last-package)))))
> >
> > Why so much hate?
> >
> > - use multiple-value-prog1 instead of prog1 that lose all the other values.
> > - use unwind-protect to restore *package* to last-package.
> > - don't use any of the above, just let:
> >
> > (defmacro with-package (package-symbol &body body)
> >    `(let ((*package* (find-package ,package-symbol)))
> >         ,@body))
> >
> > But what's the point:
> >
> > (with-package  'foo
> >    (list (read) (read)))
> >
> > vs.:
> >
> > (let ((*package* (find-package 'foo)))
> >     (list (read) (read)))
> >
> > ?
> >
> > --
> > __Pascal Bourguignon__                    http://www.informatimago.com/
> >
> > The world will now reboot.  don't bother saving your artefacts.
> 
> Your solution is simple and clean. But unfortunately it doesn't work.

You did not say what it should do.

> 
> CL-USER 82 > (defpackage "foo")
> #<PACKAGE foo>
> 
Again. "foo" is not 'foo. In Common Lisp all symbols
are read to uppercase by default.

> CL-USER 83 > (in-package :foo)
> #<PACKAGE FOO>
> 
> FOO 84 > (defun bar ())
> BAR

Now you have FOO::BAR defined.

> 
> FOO 85 > (in-package :cl-user)
> #<PACKAGE COMMON-LISP-USER>
> 
> CL-USER 86 > (let ((*package* (find-package 'foo)))
>                          (bar))





This is the same as:

(cl:let ((cl:*package* (cl:find-package 'cl-user::foo)))
                          (cl-user::bar))

Just call (READ) and enter

'(let ((*package* (find-package 'foo)))
                          (bar))

> 
> Error: Undefined function COMMON-LISP-USER::BAR called with arguments
> ().
> 
> The problem is that in the body of LET for some reasons the local
> variable *package* doesn't behave like the global variable.

It does. But your idea what it should do is wrong.
*PACKAGE* has influence on the READer and INTERN/FIND-SYMBOL,
but not on the running code.

The reason is that the code is read with READ.
It creates the data structure for the code.

Then this data-structure is given to EVAL, which executes it. The result
is then given to print.

(print (eval (read)))

So first READ, then EVAL, then PRINT.

(READ)  in a stream of characters, out a Lisp data structure

(EVAL) in a Lisp data structure, out the result of evaluating it.

Since READ already gives you all the symbols, at runtime switching
the package of *package* has no effect on the running code.



> CL-USER 82 > (defpackage "foo")
> #<PACKAGE foo>
> 
Again. "foo" is not 'foo. In Common Lisp all symbols
are read to uppercase by default.

> CL-USER 83 > (in-package :foo)
> #<PACKAGE FOO>
> 
> FOO 84 > (defun bar ())
> BAR

Now you have FOO::BAR defined.

> 
> FOO 85 > (in-package :cl-user)
> #<PACKAGE COMMON-LISP-USER>
> 
> CL-USER 86 > (let ((*package* (find-package 'foo)))
>                          (bar))

CL-USER 1 > (defpackage "FOO")
#<The FOO package, 0/16 internal, 0/16 external>

CL-USER 2 > (in-package :foo)
#<The FOO package, 0/16 internal, 0/16 external>

FOO 3 > (defun bar ())
BAR

FOO 4 > (in-package :cl-user)
#<The COMMON-LISP-USER package, 12/32 internal, 0/4 external>

CL-USER 5 > (let ((*package* (find-package 'foo)))
  (funcall (find-symbol "BAR")))
NIL

This is the only way it can work. FIND-SYMBOL uses *package*
as the package to look into.

-- 
http://lispm.dyndns.org
From: Adrian DOZSA
Subject: Re: in-package function vs macro
Date: 
Message-ID: <1178835556.366528.269820@p77g2000hsh.googlegroups.com>
On May 11, 12:59 am, Rainer Joswig <······@lisp.de> wrote:
> In article <·······················@h2g2000hsg.googlegroups.com>,
>  Adrian DOZSA <·········@gmail.com> wrote:
>
>
>
> > On May 10, 11:34 pm, Pascal Bourguignon <····@informatimago.com>
> > wrote:
> > > Thibault Langlois <·················@gmail.com> writes:
> > > > On May 10, 8:36 pm, Adrian DOZSA <·········@gmail.com> wrote:
> > > >> Here's a problem i can't figure it out.
> > > >> Let's say I want to do something like this:
>
> > > >> > (let ((name "foo"))
>
> > > >>       (in-package name)
> > > >>       (.....))
>
> > > >> The problem is that in-package is a macro and treats name as a string
> > > >> and i end up with (in-package "name").
>
> > > >> Trying to resolve the problem i ended up using the in-package
> > > >> function:
>
> > > >> > (let ((name "foo"))
>
> > > >>       (funcall #'in-package name)
> > > >>       (.....))
> > > >> But this throws a strange error (on Lispworks):
> > > >> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> > > >> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> > > >> Does anybody know what's wrong here? Or perhaps an other way to solve
> > > >> what I'm trying to do.
> > > >> Thanks.
>
> > > > Maybe you want to do:
>
> > > > (with-package 'foo
> > > >   (........))
>
> > > > (defmacro with-package (package-symbol &body body)
> > > >   (let ((last-package (gensym)))
> > > >     `(let ((,last-package *package*))
> > > >        (setq *package* (find-package ,package-symbol))
> > > >        (prog1
> > > >            (progn ,@body)
> > > >          (setq *package* ,last-package)))))
>
> > > Why so much hate?
>
> > > - use multiple-value-prog1 instead of prog1 that lose all the other values.
> > > - use unwind-protect to restore *package* to last-package.
> > > - don't use any of the above, just let:
>
> > > (defmacro with-package (package-symbol &body body)
> > >    `(let ((*package* (find-package ,package-symbol)))
> > >         ,@body))
>
> > > But what's the point:
>
> > > (with-package  'foo
> > >    (list (read) (read)))
>
> > > vs.:
>
> > > (let ((*package* (find-package 'foo)))
> > >     (list (read) (read)))
>
> > > ?
>
> > > --
> > > __Pascal Bourguignon__                    http://www.informatimago.com/
>
> > > The world will now reboot.  don't bother saving your artefacts.
>
> > Your solution is simple and clean. But unfortunately it doesn't work.
>
> You did not say what it should do.
>
>
>
> > CL-USER 82 > (defpackage "foo")
> > #<PACKAGE foo>
>
> Again. "foo" is not 'foo. In Common Lisp all symbols
> are read to uppercase by default.
>
> > CL-USER 83 > (in-package :foo)
> > #<PACKAGE FOO>
>
> > FOO 84 > (defun bar ())
> > BAR
>
> Now you have FOO::BAR defined.
>
>
>
> > FOO 85 > (in-package :cl-user)
> > #<PACKAGE COMMON-LISP-USER>
>
> > CL-USER 86 > (let ((*package* (find-package 'foo)))
> >                          (bar))
>
> This is the same as:
>
> (cl:let ((cl:*package* (cl:find-package 'cl-user::foo)))
>                           (cl-user::bar))
>
> Just call (READ) and enter
>
> '(let ((*package* (find-package 'foo)))
>                           (bar))
>
>
>
> > Error: Undefined function COMMON-LISP-USER::BAR called with arguments
> > ().
>
> > The problem is that in the body of LET for some reasons the local
> > variable *package* doesn't behave like the global variable.
>
> It does. But your idea what it should do is wrong.
> *PACKAGE* has influence on the READer and INTERN/FIND-SYMBOL,
> but not on the running code.
>
> The reason is that the code is read with READ.
> It creates the data structure for the code.
>
> Then this data-structure is given to EVAL, which executes it. The result
> is then given to print.
>
> (print (eval (read)))
>
> So first READ, then EVAL, then PRINT.
>
> (READ)  in a stream of characters, out a Lisp data structure
>
> (EVAL) in a Lisp data structure, out the result of evaluating it.
>
> Since READ already gives you all the symbols, at runtime switching
> the package of *package* has no effect on the running code.
>
> > CL-USER 82 > (defpackage "foo")
> > #<PACKAGE foo>
>
> Again. "foo" is not 'foo. In Common Lisp all symbols
> are read to uppercase by default.
>
> > CL-USER 83 > (in-package :foo)
> > #<PACKAGE FOO>
>
> > FOO 84 > (defun bar ())
> > BAR
>
> Now you have FOO::BAR defined.
>
>
>
> > FOO 85 > (in-package :cl-user)
> > #<PACKAGE COMMON-LISP-USER>
>
> > CL-USER 86 > (let ((*package* (find-package 'foo)))
> >                          (bar))
>
> CL-USER 1 > (defpackage "FOO")
> #<The FOO package, 0/16 internal, 0/16 external>
>
> CL-USER 2 > (in-package :foo)
> #<The FOO package, 0/16 internal, 0/16 external>
>
> FOO 3 > (defun bar ())
> BAR
>
> FOO 4 > (in-package :cl-user)
> #<The COMMON-LISP-USER package, 12/32 internal, 0/4 external>
>
> CL-USER 5 > (let ((*package* (find-package 'foo)))
>   (funcall (find-symbol "BAR")))
> NIL
>
> This is the only way it can work. FIND-SYMBOL uses *package*
> as the package to look into.
>
> --http://lispm.dyndns.org

Thanks for the detailed explanations. Now i've got it and learned some
valuable things.
From: Tim Bradshaw
Subject: Re: in-package function vs macro
Date: 
Message-ID: <f203fj$8ol$1$830fa7a5@news.demon.co.uk>
On 2007-05-10 21:34:22 +0100, Pascal Bourguignon <···@informatimago.com> said:

> (defmacro with-package (package-symbol &body body)
>    `(let ((*package* (find-package ,package-symbol)))
>         ,@body))

Note that the body is not a top-level form here.  That might matter.
From: Rainer Joswig
Subject: Re: in-package function vs macro
Date: 
Message-ID: <joswig-4C3376.21480210052007@news-europe.giganews.com>
In article <·······················@q75g2000hsh.googlegroups.com>,
 Adrian DOZSA <·········@gmail.com> wrote:

> Here's a problem i can't figure it out.
> Let's say I want to do something like this:
> 
> > (let ((name "foo"))

Make sure your package is named "foo". Usually
you would use uppercase, "FOO".


>       (in-package name)
>       (.....))

I'm not sure what you what to do.
What should (.....) do?


> 
> The problem is that in-package is a macro and treats name as a string
> and i end up with (in-package "name").

Not name. "NAME".

> 
> Trying to resolve the problem i ended up using the in-package
> function:

There is no IN-PACKAGE function in Common Lisp.

> 
> > (let ((name "foo"))
>       (funcall #'in-package name)
>       (.....))
> But this throws a strange error (on Lispworks):
> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
> 
> Does anybody know what's wrong here? Or perhaps an other way to solve
> what I'm trying to do.

What are you trying to do?

You know about *PACKAGE* ?

(let ((*package* (find-package "foo")))
   ...
   )


> Thanks.

-- 
http://lispm.dyndns.org
From: Pascal Bourguignon
Subject: Re: in-package function vs macro
Date: 
Message-ID: <873b24lag0.fsf@thalassa.lan.informatimago.com>
Adrian DOZSA <·········@gmail.com> writes:

> Here's a problem i can't figure it out.
> Let's say I want to do something like this:
>
>> (let ((name "foo"))
>       (in-package name)
>       (.....))
>
> The problem is that in-package is a macro and treats name as a string
> and i end up with (in-package "name").

When you have a macro, and you want to do such a thing, one brutal way
to do it is to macroexpand the macro to see what it does, and do the
same:


C/USER[30]> (macroexpand '(in-package name))
(EVAL-WHEN (:COMPILE-TOPLEVEL LOAD EVAL)
 (SETQ *PACKAGE* (SYSTEM::%FIND-PACKAGE "NAME"))) ;
T

Of course, the macroexpansion may give implementation specific
details...  But it should give you some idea.  In this case, you can do:

(let ((name "foo"))
  (setf *package* (find-package name)))

You may also consider a EVAL-WHEN, but we'd need data on the circumstances.


> Trying to resolve the problem i ended up using the in-package
> function:
>
>> (let ((name "foo"))
>       (funcall #'in-package name)
>       (.....))
> But this throws a strange error (on Lispworks):
> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> Does anybody know what's wrong here? 

Yes, we do.

> Or perhaps an other way to solve
> what I'm trying to do.

The problem is that we don't know what you're trying to do, and I'd
bet you don't know either.

Binding *PACKAGE* is effective only on INTERN, READ (and READ-FROM-STRING).

Binding *PACKAGE* (be it directly, or using the IN-PACKAGE macro)
inside a form cannot have any influence on the form itself, since the
form is read long before the binding of *PACKAGE* has any chance to be
done (read-time vs run-time).


(defun read-in-package (package-designator)
  (let ((*package* (if (packagep package-designator) 
                       package-designator
                       (find-package package-designator))))
    (read)))

(make-package "TEST1" :use '())
(make-package "TEST2" :use '())

(values
  (with-input-from-string (*standard-input* "(abc def)")
    (read-in-package "TEST1"))
  (with-input-from-string (*standard-input* "(abc def)")
    (read-in-package :test2)))

--> (TEST1::ABC TEST1::DEF) ;
    (TEST2::ABC TEST2::DEF)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
From: Kent M Pitman
Subject: Re: in-package function vs macro
Date: 
Message-ID: <usla4uqbj.fsf@nhplace.com>
Pascal Bourguignon <···@informatimago.com> writes:

> The problem is that we don't know what you're trying to do, and I'd
> bet you don't know either.
> 
> Binding *PACKAGE* is effective only on INTERN, READ (and READ-FROM-STRING).
> 
> Binding *PACKAGE* (be it directly, or using the IN-PACKAGE macro)
> inside a form cannot have any influence on the form itself, since the
> form is read long before the binding of *PACKAGE* has any chance to be
> done (read-time vs run-time).

Absolutely the correct advice to give.  Understanding and appreciating
these concepts is key to getting along in Common Lisp.

Although the adventurous reader may find the following to be interesting
counterpoint once those concepts are solidly in place:

  http://www.nhplace.com/kent/PS/Ambitious.html
From: John Thingstad
Subject: Re: in-package function vs macro
Date: 
Message-ID: <op.tr4najv6pqzri1@pandora.upc.no>
On Thu, 10 May 2007 21:36:33 +0200, Adrian DOZSA <·········@gmail.com>  
wrote:

> Here's a problem i can't figure it out.
> Let's say I want to do something like this:
>
>> (let ((name "foo"))
>       (in-package name)
>       (.....))
>
> The problem is that in-package is a macro and treats name as a string
> and i end up with (in-package "name").
>
> Trying to resolve the problem i ended up using the in-package
> function:
>
>> (let ((name "foo"))
>       (funcall #'in-package name)
>       (.....))
> But this throws a strange error (on Lispworks):
> Error: The call (#<function IN-PACKAGE 20274552> "foo") does not match
> definition (#<function IN-PACKAGE 20274552> PKG::NAME &REST REST).
>
> Does anybody know what's wrong here? Or perhaps an other way to solve
> what I'm trying to do.
> Thanks.
>

use

(setf *package* (find-package "foo"))

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/