Page 1 of 1

package confusion

Posted: Mon Dec 07, 2009 12:40 pm
by cable
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?

Re: package confusion

Posted: Mon Dec 07, 2009 1:35 pm
by ramarren
Works for me. Are you sure that package definitions have been loaded in the proper order?

Re: package confusion

Posted: Mon Dec 07, 2009 3:49 pm
by cable
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?

Re: package confusion

Posted: Mon Dec 07, 2009 4:15 pm
by cable
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?

Re: package confusion

Posted: Mon Dec 07, 2009 11:49 pm
by ramarren
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.