From: vttoonses
Subject: Help with packages
Date: 
Message-ID: <e3a33c66-1eb0-4b5f-bca8-a8c7d4150674@l42g2000hsc.googlegroups.com>
I am in the process of learning lisp and am trying to understand how
to use packages. Would someone take a moment to answer some questions
I have?

1. I understand that it is preferred to define a package (defpackage)
in one file while implementing the contents in another (starting with
in-package). Is this correct?
2. Given that #1 is correct, it appears that loading the two files
will not change the current package; it just creates a new namespace.
Correct?
3. Is it possible to define a class for export? Do I include just the
class's symbol in the export list, or do I need to include each slot
as well? If so, how?

(defclass foo ()
   ((bar :accessor abar)
    (baz :reader rbaz)))

(....
  (:export :foo
              :bar
              :baz))

- or -

(.....
  (:export :foo
              :abar
              :rbaz))

or some other method?
4. I read that java style package naming is coming into vogue (i.e
com.barugames.server.simplexml). If this is true, is there a mechanism
for defining aliases? It would seem to make code more readable.

Thanks for any help,
Gene

From: Tamas K Papp
Subject: Re: Help with packages
Date: 
Message-ID: <6ivt12Fqnn3U2@mid.individual.net>
On Fri, 12 Sep 2008 12:01:49 -0700, vttoonses wrote:

> I am in the process of learning lisp and am trying to understand how to
> use packages. Would someone take a moment to answer some questions I
> have?

I would suggest that you read 

http://www.gigamonkeys.com/book/programming-in-the-large-packages-and-
symbols.html

or possibly the whole book.  And look at the source code for some small 
packages,

> 1. I understand that it is preferred to define a package (defpackage) in
> one file while implementing the contents in another (starting with
> in-package). Is this correct?

Yes, it is customary to do that, and then have ASDF load the file with 
the package definition first.

> 2. Given that #1 is correct, it appears that loading the two files will
> not change the current package; it just creates a new namespace.
> Correct?

Yes, if the current package is something else (eg :cl-user).

> 3. Is it possible to define a class for export? Do I include just the
> class's symbol in the export list, or do I need to include each slot as
> well? If so, how?
> 
> (defclass foo ()
>    ((bar :accessor abar)
>     (baz :reader rbaz)))
> 
> (....
>   (:export :foo
>               :bar
>               :baz))
> 
> - or -
> 
> (.....
>   (:export :foo
>               :abar
>               :rbaz))
> 
> or some other method?

Depends on what you want to export.  What you export can be used from 
other packages.  I would export both slot names and accessors, if they 
are different.

> 4. I read that java style package naming is coming into vogue (i.e
> com.barugames.server.simplexml). If this is true, is there a mechanism
> for defining aliases? It would seem to make code more readable.

Some people use this, but it is merely a convention on how to make up a 
string that corresponds to the package.  It has no semantics in Common 
Lisp per se.

HTH,

Tamas
From: vttoonses
Subject: Re: Help with packages
Date: 
Message-ID: <56e2a380-dcc6-417b-9be9-b86b4172e70a@e39g2000hsf.googlegroups.com>
On Sep 12, 3:08 pm, Tamas K Papp <······@gmail.com> wrote:
> On Fri, 12 Sep 2008 12:01:49 -0700, vttoonses wrote:
> > I am in the process of learning lisp and am trying to understand how to
> > use packages. Would someone take a moment to answer some questions I
> > have?
>
> I would suggest that you read
>
> http://www.gigamonkeys.com/book/programming-in-the-large-packages-and-
> symbols.html
>
> or possibly the whole book.  And look at the source code for some small
> packages,
>
> > 1. I understand that it is preferred to define a package (defpackage) in
> > one file while implementing the contents in another (starting with
> > in-package). Is this correct?
>
> Yes, it is customary to do that, and then have ASDF load the file with
> the package definition first.
>
> > 2. Given that #1 is correct, it appears that loading the two files will
> > not change the current package; it just creates a new namespace.
> > Correct?
>
> Yes, if the current package is something else (eg :cl-user).
>
>
>
> > 3. Is it possible to define a class for export? Do I include just the
> > class's symbol in the export list, or do I need to include each slot as
> > well? If so, how?
>
> > (defclass foo ()
> >    ((bar :accessor abar)
> >     (baz :reader rbaz)))
>
> > (....
> >   (:export :foo
> >               :bar
> >               :baz))
>
> > - or -
>
> > (.....
> >   (:export :foo
> >               :abar
> >               :rbaz))
>
> > or some other method?
>
> Depends on what you want to export.  What you export can be used from
> other packages.  I would export both slot names and accessors, if they
> are different.
>
> > 4. I read that java style package naming is coming into vogue (i.e
> > com.barugames.server.simplexml). If this is true, is there a mechanism
> > for defining aliases? It would seem to make code more readable.
>
> Some people use this, but it is merely a convention on how to make up a
> string that corresponds to the package.  It has no semantics in Common
> Lisp per se.
>
> HTH,
>
> Tamas

Thanks Tamas.

I am actually reading that book right now (along with ANSI Common
List) and it is the chapter on packages that led to some of my
questions.

As to question number 3, are you saying that each symbol that I wish
to use from outside the package implementation file(s) needs to be
exported explicitly? Exporting a class name only allows the creation
of the class (without the '::' syntax) not access to the accessor/
reader/writer methods, correct? (I'm assuming this is due to the
separation of data and behavior in the CLOS).

I suppose I could also use the slot-value function on the exported
class's instance.

Again, thanks for the answers.

Gene
From: Alberto Riva
Subject: Re: Help with packages
Date: 
Message-ID: <gaehmg$6g00$1@usenet.osg.ufl.edu>
vttoonses wrote:
> 
> As to question number 3, are you saying that each symbol that I wish
> to use from outside the package implementation file(s) needs to be
> exported explicitly?

Yes, because the act of exporting a symbol only works on the symbol, it 
doesn't (and couldn't) take the semantics of the symbol into 
consideration. You could use the same symbol to name a class and a 
function; how could EXPORT know if you wanted to export the function or 
the class (with all of its slots and accessors?)

By the way, the problem doesn't have to do with files: if you define a 
class in package :foo in one file, and you reference it in a second file 
that is still in package :foo, then there's no problem. What matters is 
what package you're in when the reader sees the symbol.

> Exporting a class name only allows the creation
> of the class (without the '::' syntax) not access to the accessor/
> reader/writer methods, correct?

Exactly, because they are named by different symbols. To be more 
precise, creation of a class instance is always allowed, regardless of 
the package you're in; the only difference is whether you need to use 
the package prefix in front of the symbols or not.

> I suppose I could also use the slot-value function on the exported
> class's instance.

But SLOT-VALUE needs to slot name, that is a symbol, so you would have 
to export+import that too (or use ::).

On the other hand, of you really wanted that, you could easily write a 
macro that expands into a DEFCLASS followed by an EXPORT for all the 
slot names and accessors.

Alberto
From: Thomas A. Russ
Subject: Re: Help with packages
Date: 
Message-ID: <ymiprn824rs.fsf@blackcat.isi.edu>
vttoonses <·········@gmail.com> writes:

> As to question number 3, are you saying that each symbol that I wish
> to use from outside the package implementation file(s) needs to be
> exported explicitly? 

Correct.

> Exporting a class name only allows the creation
> of the class (without the '::' syntax) not access to the accessor/
> reader/writer methods, correct? (I'm assuming this is due to the
> separation of data and behavior in the CLOS).

No, the assumption is incorrect.

Exporting applies only to SYMBOLs.  It has nothing to do with classes,
slots, CLOS or anything else.  Packages manage only namespaces and not
any other elements of Lisp code.

So if you have a class and a function named "FOO", then exporting the
symbol FOO means that both

   (make-instance 'my-package:foo)

and

   (my-package:foo ...)

can be invoked without needing to use the double colons.

Packages in lisp are only there to prevent accidental namespace
collisions.  They do not purport to provide any measure of data or
implementation hiding.  (As the ability to access internal symbols of
packages demonstrates.)


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Leandro Rios
Subject: Re: Help with packages
Date: 
Message-ID: <48cafd23$0$1341$834e42db@reader.greatnowhere.com>
vttoonses escribi�:
> I am in the process of learning lisp and am trying to understand how
> to use packages. 

Take a look at: http://www.flownet.com/ron/packages.pdf

Hth,

Leandro
From: Kenny
Subject: Re: Help with packages
Date: 
Message-ID: <48cb0ebc$0$26973$607ed4bc@cv.net>
vttoonses wrote:
> I am in the process of learning lisp and am trying to understand how
> to use packages.
> Would someone take a moment to answer some questions
> I have?
> 
> 1. I understand that it is preferred to define a package (defpackage)
> in one file while implementing the contents in another (starting with
> in-package). Is this correct?

Pretty much. There is no harm in:

(defpackage xxx ...)

(in-package :xxx)

<a few defparameters>

All in the same file.

> 2. Given that #1 is correct, it appears that loading the two files
> will not change the current package; it just creates a new namespace.
> Correct?
> 3. Is it possible to define a class for export? Do I include just the
> class's symbol in the export list, or do I need to include each slot
> as well? If so, how?

Yep, any symbol. Note that this is just about symbols, so exporting the 
symbol that happens to be a class name does nothing in re symbols used 
for slots or anything else.

> 
> (defclass foo ()
>    ((bar :accessor abar)
>     (baz :reader rbaz)))
> 
> (....
>   (:export :foo
>               :bar
>               :baz))

Oops. Above you are exporting keywords (the colon thing) and keywords go 
in the keyword package so they do not need exporting and you are not 
exporting the other symbols from your package which I just noticed does 
not have a name. :)

Perhaps you have seen and are misremembering?:

(...
    (:export #:foo #:bar #:baz))

That notation lets one tell defpackage about the symbols you want to 
export without interning them in the package in effect while the 
defpackage form is being loaded, such as cl-user. Another way to avoid 
that is use strings, but watch the case: "FOO".

> 
> - or -
> 
> (.....
>   (:export :foo
>               :abar
>               :rbaz))
> 
> or some other method?
> 4. I read that java style package naming is coming into vogue (i.e
> com.barugames.server.simplexml). If this is true, is there a mechanism
> for defining aliases? It would seem to make code more readable.
> 
> Thanks for any help,

Packages are overrated. Use them when shipping a library to the world, 
or when your own app gets above 20kloc.

kt
From: Thomas A. Russ
Subject: Re: Help with packages
Date: 
Message-ID: <ymiljxt1eyp.fsf@blackcat.isi.edu>
Kenny <·········@gmail.com> writes:
> Pretty much. There is no harm in:
> 
> (defpackage xxx ...)
> 
> (in-package :xxx)

Except of course, for all of the brain-dead internet filtering software
that will stop your post from going through.  LOL.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Rainer Joswig
Subject: Re: Help with packages
Date: 
Message-ID: <joswig-729E1B.21240612092008@news-europe.giganews.com>
In article 
<····································@l42g2000hsc.googlegroups.com>,
 vttoonses <·········@gmail.com> wrote:

> I am in the process of learning lisp and am trying to understand how
> to use packages. Would someone take a moment to answer some questions
> I have?
> 
> 1. I understand that it is preferred to define a package (defpackage)
> in one file while implementing the contents in another (starting with
> in-package). Is this correct?
> 2. Given that #1 is correct, it appears that loading the two files
> will not change the current package; it just creates a new namespace.
> Correct?
> 3. Is it possible to define a class for export? Do I include just the
> class's symbol in the export list, or do I need to include each slot
> as well? If so, how?
> 
> (defclass foo ()
>    ((bar :accessor abar)
>     (baz :reader rbaz)))
> 
> (....
>   (:export :foo
>               :bar
>               :baz))
> 
> - or -
> 
> (.....
>   (:export :foo
>               :abar
>               :rbaz))
> 
> or some other method?
> 4. I read that java style package naming is coming into vogue (i.e
> com.barugames.server.simplexml). If this is true, is there a mechanism
> for defining aliases? It would seem to make code more readable.
> 
> Thanks for any help,
> Gene

4. Packages can have nicknames.

The basic thing you need to know is that symbols belong to packages.
Not functions, classes or slots. Just symbols. So you export
a symbol from a package. Not a class, not a function, not a variable.
There is no 'module' system where you can export/import these
things. Symbols are names and packages are namespaces for symbols.
Not more. If you export FOO from package BAR then you can
write FOO:BAR (instead of FOO::BAR). This is independent from
what FOO names - whether it is a slot name, a class name, a function
name, a variable name, or anything else.

-- 
http://lispm.dyndns.org/