I am trying to dynamically create classes after reading in their characteristics from an external file (which is similar in structure to an ini file). However, I am having problems with the creation of the classes when calling a defclass-builder macro from a function. The function is meant to create any super-classes before the passed-in class name is created. I have stripped down both the macro and the function to just the essentials that re-create the problem:
To demonstrate the problem, setup 3 nodes (programmer and person both have super-class names that are properties and mammal is a base class with a slot called "breathes" and it does not have a super-class):
When I run the defclass* macro directly from the top level, the class is created correctly; however, when I run the create-property-class function it does not. I understand why it doesn't work properly when I run the function but I don't know how to fix the problem. I've been banging my head against the wall all night trying different things but haven't been able to come up with a solution. I would appreciate any suggestions or pointers that might explain what I should be doing.
On Thu, 29 Aug 2002 04:06:00 GMT, Bill Clementson <bc19...@attbi.com> wrote: >I am trying to dynamically create classes after reading in their >characteristics from an external file (which is similar in structure >to an ini file). However, I am having problems with the creation of >the classes when calling a defclass-builder macro from a >function. The function is meant to create any super-classes before >the passed-in class name is created. I have stripped down both the >macro and the function to just the essentials that re-create the >problem:
It looks like you recognize that defclass is a macro and are trying to get around that, but your defclass* macro obviously doesn't get you anywhere. A simple and portable way to get around this is to use eval:
I'm ad-libing a bit, assuming that you want ancestor as a superclass of node. Note that you don't have to define ancestor before node as long as ancestor is defined before any nodes are instantiated.
>When I run the defclass* macro directly from the top level, >the class is created correctly; however, when I run the >create-property-class function it does not. I understand why >it doesn't work properly when I run the function but I don't >know how to fix the problem. I've been banging my head against >the wall all night trying different things but haven't been >able to come up with a solution. I would appreciate any >suggestions or pointers that might explain what I should be >doing.
If you want to do this without evaluating defclass forms you can use ensure-class from the metaobject protocol, if you implementation supports it.
Bill Clementson <bc19...@attbi.com> wrote: > I am trying to dynamically create classes after reading in their > characteristics from an external file (which is similar in structure > to an ini file).
There is code in the AMOP book that does something that may be what you want. I couldn't get it to work with CMUCL but it seems to work with Allegro Common Lisp.
(I added the mop: package prefixes to the appropriate calls for ACL. This is also necessary for CMUCL, but you have to do mop:find-class and mop:class-name as well.)
-- Fred Gilham gil...@csl.sri.com America does not know the difference between sex and money. It treats sex like money because it treats sex as a medium of exchange, and it treats money like sex because it expects its money to get pregnant and reproduce. --- Peter Kreeft
tmo...@sea-tmoore-l.dotcast.com (Tim Moore) writes: > It looks like you recognize that defclass is a macro and are trying to > get around that, but your defclass* macro obviously doesn't get you > anywhere. A simple and portable way to get around this is to use > eval:
Fred Gilham <gil...@snapdragon.csl.sri.com> writes: > Bill Clementson <bc19...@attbi.com> wrote: > > I am trying to dynamically create classes after reading in their > > characteristics from an external file (which is similar in structure > > to an ini file).
> There is code in the AMOP book that does something that may be what > you want. I couldn't get it to work with CMUCL but it seems to work > with Allegro Common Lisp.
Thanks for the suggestion - Tim Moore also make a suggestion in another post which was more in line with what I was trying to do. But thank you for considering my problem and offering an alternative.
* Bill Clementson | I am trying to dynamically create classes after reading in their character- | istics from an external file (which is similar in structure to an ini file).
I suggest that you macroexpand the `defpackageŽ form to see what it does.
-- Erik Naggum, Oslo, Norway
Act from reason, and failure makes you rethink and study harder. Act from faith, and failure makes you blame someone and push harder.
Erik Naggum <e...@naggum.no> writes: > * Bill Clementson > | I am trying to dynamically create classes after reading in their character- > | istics from an external file (which is similar in structure to an ini file).
> I suggest that you macroexpand the `defpackageŽ form to see what it does.
Hmmm, ok - I macroexpanded 'defpackage' and here is what I got:
This does give me some clues as it shows me that the macro 'defclass' uses the function 'clos::ensure-class' to create the class definition. This is what Tim Moore alluded to in his earlier email and (if I had used 'ensure-class' rather than 'defclass'), that would have been a valid approach to get around my problem.
Did you make a typo or is there something I'm missing? If it really was 'defpackage' that you thought would be useful for me to look at, please give me some idea as to what part of the macroexpansion relates specifically to my problem.
This is one of those rare cases where you really do need to use EVAL. That is because there is no portable way of creating classes with a function call -- the DEFCLASS macro is all you've got. You will need something like: