From: Jeremy
Subject: Macro Template filling question [newbie]
Date: 
Message-ID: <Pine.LNX.4.33.0204260921340.17606-100000@fajita.toad.net>
Hi,

I am trying to set up a macro that will automagically create a structure
and abstractions for accessing it's properties.  Here is what I have:

(defmacro create-structure (name @rest properties)
	`(defstruct ,name ,@properties)
	`(dolist (i ,properties)
		(defmethod get-,i ((item ,i))
		(,name-,i item))))

I know that this isn't right, but what I'm looking for is a way to combine
the filled template item with text that's already there to create a new
"word".  (ie the get-i or ,name-i)

I've looked around through the Hyperspec and 2 lisp books I have, and I
haven't found anything like what I need.  Any and all help is greatly
appreciated.

Jeremy

From: Kent M Pitman
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <sfwy9fav8j4.fsf@shell01.TheWorld.com>
Jeremy <······@iamthethinker.com> writes:

> Hi,
> 
> I am trying to set up a macro that will automagically create a structure
> and abstractions for accessing it's properties.  Here is what I have:
> 
> (defmacro create-structure (name @rest properties)

&rest, not @rest

> 	`(defstruct ,name ,@properties)

The above form will simply be discarded.  The body of a DEFMACRO is an
"implicit PROGN", that is, a series of forms to evaluate, in series, 
returning the last.  You probably want
 `(progn (defstruct ,name ,@properties)
        ,@(mapcar #'(lambda (prop)
                      `(defmethod ,(intern (format nil "~A~A" 'get- prop))
                                  ((item ,name)) ;<-- you had ,prop ??
                          (,(intern (format nil "~A~A" 'name- prop)) item)))
                  properties))
or some such thing.  [I didn't test it, but it might be at least close.
It will perhaps give you more things to look up in the HyperSpec...]

> 	`(dolist (i ,properties)
> 		(defmethod get-,i ((item ,i))
> 		(,name-,i item))))
> 
> I know that this isn't right, but what I'm looking for is a way to combine
> the filled template item with text that's already there to create a new
> "word".  (ie the get-i or ,name-i)
> 
> I've looked around through the Hyperspec and 2 lisp books I have, and I
> haven't found anything like what I need.  Any and all help is greatly
> appreciated.

I have some doubts about whether you actually want to be writing macros
that expand to methods like this, since I'm betting you're fighting 
object-oriented inheritance and just don't need this many different methods
that just do nothing but call another.  But that's a separate question.
I've tried  to address the above to what you seem to be trying to do...
From: Jeremy Whetzel
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <87sn5h3f6s.fsf@iamthethinker.com>
Kent M Pitman <······@world.std.com> writes:


> > (defmacro create-structure (name @rest properties)
> 
> &rest, not @rest

Eh, yeah... that's what I get for trying to type it out while listening
for the boss to come around the corner.  =0)
 

> > 	`(defstruct ,name ,@properties)
> 
> The above form will simply be discarded.  The body of a DEFMACRO is an
> "implicit PROGN", that is, a series of forms to evaluate, in series, 
> returning the last. 

I had tried just the part up to the above line under Clisp, and it
seemed to work.  Perhaps I'm just not understanding what you mean by
'discarded'.


> You probably want
>  `(progn (defstruct ,name ,@properties)
>         ,@(mapcar #'(lambda (prop)
>                       `(defmethod ,(intern (format nil "~A~A" 'get- prop))
>                                   ((item ,name)) ;<-- you had ,prop ??
>                           (,(intern (format nil "~A~A" 'name- prop)) item)))
>                   properties))
> or some such thing.  [I didn't test it, but it might be at least close.
> It will perhaps give you more things to look up in the HyperSpec...]

Yes, I believe this is where I'm trying to go with it.  (I haven't
tested it yet, either, but I understand what's going on in it.)  I thank
you.


> I have some doubts about whether you actually want to be writing macros
> that expand to methods like this, since I'm betting you're fighting 
> object-oriented inheritance and just don't need this many different methods
> that just do nothing but call another.  But that's a separate question.
> I've tried  to address the above to what you seem to be trying to do...

I see what you're saying.  I figured it would be a bit unorthodox, but I
was trying it for a couple of reasons.  I'm still learning the fine
points of using structures and classes, and which is better depending on
what I'm doing.  Because of this, I found myself trying one way, and
then changing and trying another, and therefore breaking most of the
program by doing so.  I started writing the get-* and set-* methods as a
way of dealing with that.  Then I realized I hated writing each one out,
and starting thinking it would be nice to have them created
automatically.  It isn't really so much about the defstruct or defclass
as it is about the property accessing macros.  Also, at this point, I'm
not dealing with inheritance.  I know that once I do, that will be a
different story.

Thanks for pointing out the concerns, though.  The insight really helps.

Jeremy
From: Coby Beck
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <vYey8.36943$zj6.1022158@news2.calgary.shaw.ca>
Jeremy <······@iamthethinker.com> wrote in message
·············································@fajita.toad.net...
> Hi,
>
> I am trying to set up a macro that will automagically create a structure
> and abstractions for accessing it's properties.

defstruct already does that...  If you need methods defined rather than
functions, why not use defclass?  defclass also lets you pick whatever
accessor names you want.  defstruct is nice for all the free stuff it gives
you, but IMO as soon as you start wanting to fight it, it's time to use
defclass.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")

....
> Here is what I have:
>
> (defmacro create-structure (name @rest properties)
> `(defstruct ,name ,@properties)
> `(dolist (i ,properties)
> (defmethod get-,i ((item ,i))
> (,name-,i item))))
From: Jeremy Whetzel
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <87ofg53etk.fsf@iamthethinker.com>
"Coby Beck" <·····@mercury.bc.ca> writes:

> Jeremy <····@iamthethinker.com> wrote:

> > Hi,
> >
> > I am trying to set up a macro that will automagically create a structure
> > and abstractions for accessing it's properties.
> 
> defstruct already does that...  If you need methods defined rather than
> functions, why not use defclass?  defclass also lets you pick whatever
> accessor names you want.  defstruct is nice for all the free stuff it gives
> you, but IMO as soon as you start wanting to fight it, it's time to use
> defclass.

As I posted in response to Mr. Pitman, that's what I get for trying to
post while at work.  The kind of thing that I'm trying to do is define
methods that will allow me to easily switch back and forth between
structures and classes, mainly because I'm still trying to learn the
fine points of when to use either one.  I kept breaking my program by
switching a structure to a class, or vice versa, and forgetting to
change the property accessor (ie, (person-name person1)) to match the
object type I was using.  I thought it would be easier if I tied that in
to one spot in the program and then left it so I wouldn't have to worry
about it again.  The reasons why I was trying to make a macro out of it
was 1) to learn how to make macros, and 2) to take the tedious portions
out of defining the methods.

Thanks for your pointer of classes vs. structures.  I think that helps
my decision making on it a bit.

Jeremy
From: Coby Beck
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <PGqy8.35246$MT2.1353248@news3.calgary.shaw.ca>
Jeremy Whetzel <····@iamthethinker.com> wrote in message
···················@iamthethinker.com...
> "Coby Beck" <·····@mercury.bc.ca> writes:
>
> > Jeremy <····@iamthethinker.com> wrote:
>
> > > Hi,
> > >
> > > I am trying to set up a macro that will automagically create a
structure
> > > and abstractions for accessing it's properties.
> >
> > defstruct already does that...  If you need methods defined rather than
> > functions, why not use defclass?  defclass also lets you pick whatever
> > accessor names you want.  defstruct is nice for all the free stuff it
gives
> > you, but IMO as soon as you start wanting to fight it, it's time to use
> > defclass.
>
[snip]
> object type I was using.  I thought it would be easier if I tied that in
> to one spot in the program and then left it so I wouldn't have to worry
> about it again.  The reasons why I was trying to make a macro out of it
> was 1) to learn how to make macros, and 2) to take the tedious portions
> out of defining the methods.

That is just the kind of thing that makes you go "wow" about lisp macros!
(once it works!  ;-)

> Thanks for your pointer of classes vs. structures.  I think that helps
> my decision making on it a bit.

Great.  That is a decision that comes up alot...and for me, the burnt hand
teaches the best lesson!  (and it is usually an accesor name used up by a
defstruct that bites me.)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Mean Gene
Subject: Re: Macro Template filling question [newbie]
Date: 
Message-ID: <87662etdxq.fsf@localhost.i-did-not-set--mail-host-address--so-tickle-me>
I think clos-utils does what you're looking for.  Check it out at

  http://www.apl.jhu.edu/~hall/lisp/CLOS-Utilities.lisp

--ET.