From: bradb
Subject: Trying to understand packages
Date: 
Message-ID: <1133942418.351695.218840@o13g2000cwo.googlegroups.com>
I am using SBCL.  My test file is

; (compile-file "package-test.lisp")
; (load "package-test")
(defpackage "package-test"
   (:use #:common-lisp))
(in-package #:package-test)

(defun test-func-p1 ()
 (format t "A func in package 1~%"))

(defpackage "package-test2"
   (:use #:common-lisp "package-test"))
(in-package #:package-test2)

;(test2)
(defun test2 ()
 (test-func-p1))

When I compile the file, SBCL complains about test-func-p1 not
existing, shouldn't this work because it is in package-test & I am
using that package?
I am totally confused, a little package enlightenment would be very
much appreciated.

Thanks
Brad

From: Barry Wilkes
Subject: Re: Trying to understand packages
Date: 
Message-ID: <1133949627.926333.157320@g49g2000cwa.googlegroups.com>
Packages can seem a bit confusing at first.  Have a read of

http://www.flownet.com/gat/packages.pdf
From: bradb
Subject: Re: Trying to understand packages
Date: 
Message-ID: <1133974216.837292.272510@g44g2000cwa.googlegroups.com>
Thanks to all that replied.  I did manage to get it working for me in
the end, I used symbols for my package names.
Here is a reply that I got off-list that is extremely useful.  I've
removed the name incase the reply was deliberately off-list.  Thanks a
lot to that person though.

Post:
On 7 Dec 2005 00:00:18 -0800, bradb  wrote:

> I am using SBCL.  My test file is
> ; (compile-file "package-test.lisp")
> ; (load "package-test")
> (defpackage "package-test"
>    (:use #:common-lisp))
> (in-package #:package-test)

> (defun test-func-p1 ()
>  (format t "A func in package 1~%"))

> (defpackage "package-test2"
>    (:use #:common-lisp "package-test"))
> (in-package #:package-test2)

> ;(test2)
> (defun test2 ()
>  (test-func-p1))

> When I compile the file, SBCL complains about test-func-p1 not
> existing, shouldn't this work because it is in package-test & I am
> using that package?
> I am totally confused, a little package enlightenment would be very
> much appreciated.

First off, you created a package called "package-test", but the follow
IN-PACKAGE form tried to use a package named "PACKAGE-TEST".  You
should have got an error at that point, unless you've done something
funny to the reader.  When you name packages and symbols, be sure to
always use UPPER-CASE (or use symbols, etc., as you did in the
IN-PACKAGE form...they're "string designators" which designate the
string that is their name, which will usually be in upper case.  The
_correct_ way is to use strings, but many people nowadays use
uninterned strings (#:whatever) to cope with Franz's horrible "modern
mode" abomination)

Secondly, the symbol TEST-FUNC-P1 is not exported by the PACKAGE-TEST
package, so it won't be automatically visible in the PACKAGE-TEST2
package merely through the use of the :USE directive.  You either have
to import it or state it explicitly wherever you use it, using the
double colon notation; e.g.,

 (defun test2 ()
   (package-test::test-func-p1))

although in your case you'd have to use

 (defun test2 ()
   (|package-test|::test-func-p1))

or similar, because the package name is in lower case.


This is what you meant:

 (defpackage "PACKAGE-TEST"     ; or #:package-test if you prefer
[blech]
   (:use "COMMON-LISP")
   (:export "TEST-FUNC-P1"))

 (defpackage "PACKAGE-TEST2"
   (:use "COMMON-LISP" "PACKAGE-TEST"))

 (in-package "PACKAGE-TEST")

 (defun test-func-p1 ()
   (format t "~&A function in package 1"))

 (in-package "PACKAGE-TEST2")

 (defun test2 ()
   (test-func-p1))


Fairly minor style issues:

 1) define both packages up front

 2) normal Lisp style is to use ~& (fresh-line) before printing,
    rather than ~% (terpri) afterwards.

Warning:

 Note that the text printed in TEST-FUNC-P1 is inaccurate.  It is
 /not/ a "function in package 1" -- packages are a /data/ issue, not
 a /code/ issue: symbols (data elements) live in packages, not
 functions (code elements).  The /function/ is not "in" any package,
 only the symbol that names it in the source code is.  Of course you
 can have many symbols naming the same function, and each can be in a
 different package!
From: Peter Herth
Subject: Re: Trying to understand packages
Date: 
Message-ID: <dn65l4$5p1$03$1@news.t-online.com>
bradb wrote:
> I am using SBCL.  My test file is
> 
> ; (compile-file "package-test.lisp")
> ; (load "package-test")
> (defpackage "package-test"
>    (:use #:common-lisp))
> (in-package #:package-test)
> 
> (defun test-func-p1 ()
>  (format t "A func in package 1~%"))
> 
> (defpackage "package-test2"
>    (:use #:common-lisp "package-test"))
> (in-package #:package-test2)
> 
> ;(test2)
> (defun test2 ()
>  (test-func-p1))
> 
> When I compile the file, SBCL complains about test-func-p1 not
> existing, shouldn't this work because it is in package-test & I am
> using that package?
> I am totally confused, a little package enlightenment would be very
> much appreciated.

You did mostly right, but you have to be *really* carefull how to 
specify symbols in defpackage statements. If you give them as strings, 
use uppercase letters only like in (defpackage "PACKAGE-TEST" ...

Peter

-- 
Ltk, the easy lisp gui http://www.peter-herth.de/ltk/
From: Christophe Rhodes
Subject: Re: Trying to understand packages
Date: 
Message-ID: <sqiru1tged.fsf@cam.ac.uk>
Peter Herth <·······@t-online.de> writes:

> bradb wrote:
>> (defpackage "package-test"
>>    (:use #:common-lisp))
>> (defpackage "package-test2"
>>    (:use #:common-lisp "package-test"))
>> I am totally confused, a little package enlightenment would be very
>> much appreciated.
>
> You did mostly right, but you have to be *really* carefull how to 
> specify symbols in defpackage statements. If you give them as strings, 
> use uppercase letters only like in (defpackage "PACKAGE-TEST" ...

In addition to this, using a package means that its exported symbols
become accessible without package qualifiers -- not that _all_ of its
symbols are accessible.  In this case, the symbol
PACKAGE-TEST::TEST-FUNC-P1 is internal to the package PACKAGE-TEST
(well, package-test, rather :-) and so the fact that package-test2
uses package-test does not make the symbol accessible.

Christophe