From: Rand Sobriquet
Subject: Macroexpansion
Date: 
Message-ID: <1e249696.0208110725.4be18030@posting.google.com>
I'm confused about some aspects of macroexpansion.  

I have this piece of code:

(capi:define-interface test-gui ()
  ()
  (:panes
    (test-layout capi:column-layout)))

(capi:display (make-instance 'test-gui))

it doesn't do anything, but it does compile.

Now I have something like this:

(defmacro expand-layout ()
  `(test-layout capi:column-layout))

(capi:define-interface test-gui ()
  ()
  (:panes
    (expand-layout)))

(capi:display (make-instance 'test-gui))


And I receive this error:

(EXPAND-LAYOUT) does not match (CAPI::NAME CLASS
&REST CAPI::INIT-OPTIONS).

I had always thought that there would be text substitution in this
case and that (expand-layout) would be replaced with (test-layout
capi:column-layout) before being expanded by the define-interface
macro.  My mental model from the examples that I have read say that
this should work.  The particular problem is not important - I just
not sure why it is not correct.  Pardon me for asking what is probably
basic question.

Rand

From: Rainer Joswig
Subject: Re: Macroexpansion
Date: 
Message-ID: <joswig-D4140A.19054011082002@news.fu-berlin.de>
In article <····························@posting.google.com>,
 ··········@eudoramail.com (Rand Sobriquet) wrote:

> I'm confused about some aspects of macroexpansion.  
> 
> I have this piece of code:
> 
> (capi:define-interface test-gui ()
>   ()
>   (:panes
>     (test-layout capi:column-layout)))
> 
> (capi:display (make-instance 'test-gui))
> 
> it doesn't do anything, but it does compile.
> 
> Now I have something like this:
> 
> (defmacro expand-layout ()
>   `(test-layout capi:column-layout))
> 
> (capi:define-interface test-gui ()
>   ()
>   (:panes
>     (expand-layout)))
> 
> (capi:display (make-instance 'test-gui))
> 
> 
> And I receive this error:
> 
> (EXPAND-LAYOUT) does not match (CAPI::NAME CLASS
> &REST CAPI::INIT-OPTIONS).
> 
> I had always thought that there would be text substitution

Macros don't work over "text". When the macro gets
control, the form is already read.

In this case the form is not macroexpanded in that
place.

> in this
> case and that (expand-layout) would be replaced with (test-layout
> capi:column-layout) before being expanded by the define-interface
> macro.  My mental model from the examples that I have read say that
> this should work.  The particular problem is not important - I just
> not sure why it is not correct.  Pardon me for asking what is probably
> basic question.

You can always check macro expansion for yourself.

(macroexpand '(foo bar (baz))  macroexpands the form
 and returns the result.

(macroexpand-1 '(foo bar (baz))  macroexpands the form
just once and returns the result.

The LispWorks IDE has some commands for that. You can
macroexpand forms with a keystroke or from the
editor menu.

LispWorks also lets you fully expand an expression
on all possible levels ("walk").
From: Kalle Olavi Niemitalo
Subject: Re: Macroexpansion
Date: 
Message-ID: <87y9bdnzb6.fsf@Astalo.y2000.kon.iki.fi>
··········@eudoramail.com (Rand Sobriquet) writes:

> I had always thought that there would be text substitution in this
> case and that (expand-layout) would be replaced with (test-layout
> capi:column-layout) before being expanded by the define-interface
> macro.

I am not familiar with CAPI, so I'll use structures instead.

  (defmacro expand-count ()
    '(count 0 :type integer))

  (defstruct foo
    (expand-count))

This defines FOO as a structure with one slot EXPAND-COUNT.
It also defines an accessor FOO-EXPAND-COUNT for the slot.
The macro definition of EXPAND-COUNT has no effect here.
Lisp does not expand the list (expand-count) as a macro form,
because the list is not in a place where it would be evaluated.

If you do

  (defparameter *thingy* '(expand-count))

the value of *THINGY* will likewise be (expand-count), not
(count 0 :type integer).
From: Barry Margolin
Subject: Re: Macroexpansion
Date: 
Message-ID: <j8Q59.5$h1.1801@paloalto-snr1.gtei.net>
In article <····························@posting.google.com>,
Rand Sobriquet <··········@eudoramail.com> wrote:
>Now I have something like this:
>
>(defmacro expand-layout ()
>  `(test-layout capi:column-layout))
>
>(capi:define-interface test-gui ()
>  ()
>  (:panes
>    (expand-layout)))
>
>(capi:display (make-instance 'test-gui))
>
>
>And I receive this error:
>
>(EXPAND-LAYOUT) does not match (CAPI::NAME CLASS
>&REST CAPI::INIT-OPTIONS).
>
>I had always thought that there would be text substitution in this
>case and that (expand-layout) would be replaced with (test-layout
>capi:column-layout) before being expanded by the define-interface
>macro.  My mental model from the examples that I have read say that
>this should work.  The particular problem is not important - I just
>not sure why it is not correct.  Pardon me for asking what is probably
>basic question.

Macros are only expanded in places where evaluation normally takes place.
It's a part of the process of evaluating a list: if the function position
contains a macro name, replace the list with its expansion; if it contains
a function name, evaluate the arguments and then call the function.

Since the parameters to the :PANES option are treated as literal lists, not
evaluated, macroexpansion does not occur there.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.