package confusion

Discussion of Common Lisp
Post Reply
cable
Posts: 4
Joined: Sat May 02, 2009 1:28 pm

package confusion

Post by cable » Mon Dec 07, 2009 12:40 pm

Hello all,

I am experimenting with creating my own set of packages that my own software can use instead of the cl package. This, of course, depends on actually being able to use the common-lisp symbols, just controlling how they're exported.

So I have my base package, called "PRELUDE.BASE", that imports cl, adds some functions and exports most of CL again. The issue comes when I use defun. The following function, defined in "PRELUDE.STRING" does not work as expected:

Code: Select all

(defun join (strings &optional token)
  (let ((format (if token
		    (format nil "~~{~~a~~^~a~~}" token)
		    "~{~a~}")))
    (format nil format strings))),
The defpackage from "PRELUDE.BASE" is:

Code: Select all

(defpackage #:prelude.base
  (:nicknames #:base)
  (:use #:cl)
  (:shadow #:get #:set)
  (:export
   "&ALLOW-OTHER-KEYS"
   "&AUX"
   "&BODY"
   "&ENVIRONMENT"
   "&KEY"
   "&OPTIONAL"
   "&REST"
   "&WHOLE"
   #:defun
   #:let
   #:if
   #:nil
   #:list
   #:first
   #:format))
And finally, "PRELUDE.STRING" defpacakge:

Code: Select all

(defpackage #:prelude.string
  (:nicknames #:string #:str)
  (:use #:prelude.base)
  (:export 
   #:join))

The reason this does not work is because &OPTIONAL is (apparently) not recognized as CL:&OPTIONAL, so it's interpreted as just a variable name. Changing the above join definition to:

Code: Select all

(defun join (strings cl:&optional token)
  (let ((format (if token
		    (format nil "~~{~~a~~^~a~~}" token)
		    "~{~a~}")))
    (format nil format strings))),
causes join to function as expected.

I'm greatly confused by this. When I switch, in slime, to the "PRELUDE.STRING" package and do (symbol-package '&OPTIONAL) it reports the "COMMON-LISP" package. Anyone know what the issue could be?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: package confusion

Post by ramarren » Mon Dec 07, 2009 1:35 pm

Works for me. Are you sure that package definitions have been loaded in the proper order?

cable
Posts: 4
Joined: Sat May 02, 2009 1:28 pm

Re: package confusion

Post by cable » Mon Dec 07, 2009 3:49 pm

No, I'm not. I'm using SBCL on Mac (latest one in ports). I have my prelude.base defsystem that depends on nothing and a prelude.string system that depends on prelude.base. You tried join without the cl:&optional? And str:join was able to be called with one argument?

cable
Posts: 4
Joined: Sat May 02, 2009 1:28 pm

Re: package confusion

Post by cable » Mon Dec 07, 2009 4:15 pm

Oh dear, well this is embarrasing. It's working now. I'm not sure why, I killed slime and restarted several times. But now everything seems to be working as it should.

How is the best way to export & symbols? I'm using "&OPTIONAL" for now because that's how it showed up in (descirbe (find-package 'cl)) but can it be done with the #: syntax?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: package confusion

Post by ramarren » Mon Dec 07, 2009 11:49 pm

cable wrote:It's working now. I'm not sure why
My guess would be you somehow polluted one of the packages with a symbol interned by the reader from the REPL. That sort of think happens when doing anything with packages, it is usually good to either delete them and reload, or even better restart the image and reload, when doing changes affecting package connections.
cable wrote:How is the best way to export & symbols? I'm using "&OPTIONAL" for now because that's how it showed up in (descirbe (find-package 'cl)) but can it be done with the #: syntax?
The '&' character is not special in any way. You can just use #:&optional, like with every other symbol.

Post Reply