I can't find out precisely why this code works:
(make-package 'pkg :use '(common-lisp))
(in-package pkg)
(defun test () (print "This is pkg:test.") (terpri))
(export '(test))
(in-package :cl-user)
(pkg:test)
while the following, in which I simply turned the first paragraph into a
function call, doesn't. I suspect it has something to do with how the
compiler decides what is to be executed when, but I don't really
understand it. Can anybody help, and/or suggest a good reading on that
topic?
(defun defun-pkg-test ()
(make-package 'pkg :use '(common-lisp))
(in-package pkg)
(defun test () (print "This is pkg:test.") (terpri))
(export '(test)))
(defun-pkg-test)
(in-package :cl-user)
(pkg:test)
Ingvar wrote:
> In the second case, the (defun test ...) form is read in whatever
> package was current on the reading of DEFUN-PKG-TEST and since the
> (presumed) creation of package PKG happens in there, there's no chance
> of the TEST inside taht form to be read as PKG::TEST.
OK. I feel I'm beginning to get it. Let me try to formulate it another way:
The characters "test" in "(defun test ...)" are read as the symbol
CL-USER:TEST (assuming we're in CL-USER) _when the function definition
is read_, that is, *before* the function is called.
When the function is called, IN-PACKAGE can have no effect on the
interpretation of any symbols.
Is this right?
PS: Thank you for the fast reply! :)
Philippe Lorin wrote:
> Ingvar wrote:
>
>> In the second case, the (defun test ...) form is read in whatever
>> package was current on the reading of DEFUN-PKG-TEST and since the
>> (presumed) creation of package PKG happens in there, there's no chance
>> of the TEST inside taht form to be read as PKG::TEST.
>
>
> OK. I feel I'm beginning to get it. Let me try to formulate it another way:
>
> The characters "test" in "(defun test ...)" are read as the symbol
> CL-USER:TEST (assuming we're in CL-USER) _when the function definition
> is read_, that is, *before* the function is called.
> When the function is called, IN-PACKAGE can have no effect on the
> interpretation of any symbols.
>
> Is this right?
Yes it is. You got it.
Cheers
--
Marco
This seems to work for me, but can anybody spot issues with this usage?
(defun make-pkg ()
(make-package 'pkg :use '(common-lisp))
(in-package :pkg)
(let ((new-test (intern "TEST")))
(setf (symbol-function new-test)
(lambda ()
(print "This is pkg::test.")
(terpri)))
(export new-test))
(in-package :cl-user)
(pkg:test))
Harold wrote:
> This seems to work for me, but can anybody spot issues with this usage?
>
> (defun make-pkg ()
> (make-package 'pkg :use '(common-lisp))
> (in-package :pkg)
> (let ((new-test (intern "TEST")))
> (setf (symbol-function new-test)
> (lambda ()
> (print "This is pkg::test.")
> (terpri)))
> (export new-test))
> (in-package :cl-user)
> (pkg:test))
>
Try to start a new fresh CL and paste the code into it. You will get
two errors (just by eyeballing it)
Cheers
--
Marco
On 9232 day of my life ·······@gmail.com wrote:
> This seems to work for me, but can anybody spot issues with this usage?
>
> (defun make-pkg ()
> (make-package 'pkg :use '(common-lisp))
> (in-package :pkg)
> (let ((new-test (intern "TEST")))
> (setf (symbol-function new-test)
> (lambda ()
> (print "This is pkg::test.")
> (terpri)))
> (export new-test))
> (in-package :cl-user)
> (pkg:test))
1.
(defmacro make-pkg ()
(make-package 'pkg :use '(common-lisp))
(let ((new-test (intern "TEST" 'pkg)))
(export new-test 'pkg)
`(progn
(defun ,new-test
(print "This is pkg::test.")
(terpri))
(pkg:test))))
2. Do you really need make-pkg? Just write into file:
(make-package 'pkg :use '(common-lisp))
(in-package 'pkg)
(defun test ()
(print "This is ...")
(terpri))
(export 'test)
(in-package :common-lisp)
(pkg:test)
--
Ivan Boldyrev
Your bytes are bitten.
Philippe Lorin <············@gmail.com> writes:
> Ingvar wrote:
>> In the second case, the (defun test ...) form is read in whatever
>> package was current on the reading of DEFUN-PKG-TEST and since the
>> (presumed) creation of package PKG happens in there, there's no chance
>> of the TEST inside taht form to be read as PKG::TEST.
>
> OK. I feel I'm beginning to get it. Let me try to formulate it another way:
>
> The characters "test" in "(defun test ...)" are read as the symbol
> CL-USER:TEST (assuming we're in CL-USER) _when the function definition
> is read_, that is, *before* the function is called.
> When the function is called, IN-PACKAGE can have no effect on the
> interpretation of any symbols.
>
> Is this right?
Yes. You might use (defun pkg::test ...), but if you try to read it
before the package pkg is defined you're doomed too.
In that case, you'll have to use (find-symbol "TEST" "PKG")
or (intern "TEST" "PKG"), but then not passing this as a
non-evaluated argument of a macro.
If you have a look at (macroexpand-1 '(defun test () "test"))
you'll see that defun may do a lot of thing that it would be inconvenient
not to do if you used just (setf (symbol-function (intern "TEST" "PKG"))
(lambda () "test"))
Either you need them or not.
If you need them then it's a case when you have to use eval.
If you don't, then you can use (setf symbol-function).
(in-package :cl-user)
(defun defun-pkg-test ()
(make-package 'pkg :use '(common-lisp))
;; in-package is useless
(eval `(defun ,(intern "TEST" "PKG") ()
(print "This is pkg:test.") (terpri)))
(export (find-symbol "TEST" "PKG") "PKG"))
(defun-pkg-test)
(pkg:test)
--
__Pascal Bourguignon__ http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.