Re: Using macro to define macros and functions in the current package?
On Thu, Apr 16, 2009 at 03:26:20PM +0300, Mikko Ahonen wrote:
> I have been experimenting with macros, packages and symbols, and it
> seems there may be something I do not quite understand.
>
> Let's say I have a packages :util and :zip. :util is utilities
> package, which has a macro-defining macro foo, which I call from
> package :zip. I would expect that the calls interns the symbols to
> package :zip but it seems it interns them into :util.
Calling UTIL::FOO in your example doesn't actually intern any new
symbols. The symbols are interned when they're read, which happens
only once, when you define the macro FOO, in package UTIL. When you
invoke FOO, it macro-expands into (in this case) a constant list with
already-interned symbols in it.
> In the example below I would expect to see symbols ZIP::BAR and
> ZIP::Y in the macroexpansion, not UTIL::BAR and UTIL::Y. Can
> somebody explain why this is so? Is there a way to prevent this?
>
> CL-USER 61 : 1 > (defpackage :util (:use :common-lisp))
> #<The UTIL package, 0/16 internal, 0/16 external>
>
> NAPLES 62 : 1 > (in-package :util)
> #<The UTIL package, 0/16 internal, 0/16 external>
>
> UTIL 63 : 1 > (defmacro foo ()
> `(defun bar (y)
> (format t "y=~A~%" y)))
> FOO
This is similar to
(in-package :util)
(defmacro foo ()
(make-foo-definition))
(defun make-foo-definition ()
'(defun bar (y)
(format t "y=~A~%" y)))
You wouldn't expect calling MAKE-FOO-DEFINITION to return symbols in
any package but UTIL, would you? So macro-expanding FOO necessarily
returns a list with symbols in UTIL.
Hope that helps.
-- Larry