Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

package blues: load a file into a package

13 views
Skip to first unread message

Mirko

unread,
Dec 17, 2008, 5:19:38 PM12/17/08
to
Here is the story:

I have a package A. I would like to load a file B.lisp that has no
package definitions. However when (in-package :A), B's symbols are
invisible. They are in :cl-user.

I tried putting (load #P/"/path-to-B/B") inside A.lisp thinking (I
mean hoping) that this would include B's symbols into package A, but
that did not work.

I could define a package B, export its symbols and import them into A,
but it is an overkill for the small file B. In addition, B is a
source file generated by Andrew Smith (state machine), and I don't
want to change it.

I could keep A without its package (keep it in cl-user), but it
depends on other packages, and I do not know how to import the
`public' symbols from those other packages into cl-user.

Any other options?

Thank you,

Mirko

Tamas K Papp

unread,
Dec 17, 2008, 5:25:48 PM12/17/08
to
On Wed, 17 Dec 2008 14:19:38 -0800, Mirko wrote:

> I tried putting (load #P/"/path-to-B/B") inside A.lisp thinking (I mean
> hoping) that this would include B's symbols into package A, but that did
> not work.

But I think it should, unless you do something to *package* inside B.lisp.

> Any other options?

You could put an (in-package :a) inside B.lisp.

Tamas

Rainer Joswig

unread,
Dec 17, 2008, 5:44:19 PM12/17/08
to
In article
<b33781f8-05a2-4f1c...@e24g2000vbe.googlegroups.com>,
Mirko <Mirko....@gmail.com> wrote:

> Here is the story:
>
> I have a package A. I would like to load a file B.lisp that has no
> package definitions. However when (in-package :A), B's symbols are
> invisible. They are in :cl-user.

If they are in CL-USER, then *package* really is CL-USER
when you load that package.

>
> I tried putting (load #P/"/path-to-B/B") inside A.lisp thinking (I
> mean hoping) that this would include B's symbols into package A, but
> that did not work.

What exactly did you do? When compile file a and it
has a load statement, nothing happens.
You have to tell the compiler that it should load
it (using EVAL-WHEN).

Anyway a simple example with load:

RJMBP:tmp joswig$ cat a.lisp
(defpackage "A" (:use "CL"))

(in-package "A")

(load "b.lisp")

(b)

RJMBP:tmp joswig$ cat b.lisp
(defun b ()
(print 'hi-there))

RJMBP:tmp joswig$ cmucl

* (load "a.lisp")

; Loading #P"/private/tmp/a.lisp".
;; Loading #P"/private/tmp/b.lisp".

HI-THERE
T
*


>
> I could define a package B, export its symbols and import them into A,
> but it is an overkill for the small file B. In addition, B is a
> source file generated by Andrew Smith (state machine), and I don't
> want to change it.
>
> I could keep A without its package (keep it in cl-user), but it
> depends on other packages, and I do not know how to import the
> `public' symbols from those other packages into cl-user.
>
> Any other options?
>
> Thank you,
>
> Mirko

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

budden

unread,
Dec 17, 2008, 5:56:23 PM12/17/08
to
Yeah, the best solution is to put (in-package :a) to b.lisp. Yes, I
know you can't. Really?
1. You can read Andrew Smith manual. If it is a good tool, it should
be able to issue defpackage form. Maybe some
configuration option help.
2. If not, you might patch codegenerator itself.
3. If not, just make new file b.tmp.lisp from b.lisp, where (in-
package :a) is prepended to b.lisp contents
and load b.tmp.lisp instead.
4. After all, you can define your own version of load.
Simplest thing is like this:

(defun my-load (file-name) (let ((*package* (find-package :iterate)))
(with-open-file (in file-name)
(loop for s = (read in nil nil)
unless s do (loop-finish)
do (eval s)))))

Note you lose ability to compile here. If you want better solution,
set up slime, sbcl/ccl and navigate through implementation sources -
you'll be able to borrow code from there.

--------------------------------------------------------------
Budden, $5/hour lisp freelancer. Hire me until it is too
late

Tim Bradshaw

unread,
Dec 18, 2008, 2:58:57 AM12/18/08
to
On Dec 17, 10:19 pm, Mirko <Mirko.Vuko...@gmail.com> wrote:

> I tried putting (load #P/"/path-to-B/B") inside A.lisp thinking (I
> mean hoping) that this would include B's symbols into package A, but
> that did not work.
>

Bind *PACKAGE* to the right package before doing this.

budden

unread,
Dec 18, 2008, 3:47:38 AM12/18/08
to
> Bind *PACKAGE* to the right package before doing this.
O-oo-ps. I was wrong :)

Mirko

unread,
Dec 18, 2008, 12:20:34 PM12/18/08
to

I tried Rainer's simple example and it does work. The loaded file is
in the package of the parent (hyperspec says the same thing).

But in my case I am still having trouble. Evidently my problem lies
somewhere else. For now, I did the `dirty' thing, and inserted a (in-
package ...) statement in the loaded file.

I will try to dig into the issue later.

Thanks to all the replied,

Mirko

budden

unread,
Dec 18, 2008, 5:49:26 PM12/18/08
to
You might also try
(let ((*package* (find-package :a)))
(load "b"))
This is how I understand Tim Bradshaw's recommendation.
0 new messages