Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Runtime class definition?

156 views
Skip to first unread message

Alberto Riva

unread,
Sep 14, 2012, 12:48:56 PM9/14/12
to
A question for the CLOS crowd out there...

Let's say I have a collection of a relatively large number of classes
P1, P2... Pn, all related to each other, and then a smaller number m of
unrelated classes, for example just two Q1, Q2. My program needs to
create instances that inherit from one of the P classes and one of the Q
classes. For example, P could indicate an analysis method, and Q could
be used to specify the type of output desired (text, html, etc). The
specific pair of P and Q classes is determined at runtime based on user
input.

Obviously I could create all the n x m combinations of classes that
inherit from one of the Ps and one of the Qs, and then instantiate the
appropriate one once I know what the user wants. But I would have to
create a large number of classes without knowing whether they'll ever
get used. It's the most straightforward solution, but it's not very
elegant, and it has the drawback that if I one day create a new Q class,
I also have to define n new derived subclasses.

So, the alternative would be to create the desired class at runtime,
using ENSURE-CLASS and passing the desired P and Q as superclasses. And
my question is: is there any drawback to this solution? I'm thinking in
particular in terms of efficiency... I know the answer is
implementation-dependent, but could this prevent optimization of method
calls?

Comments appreciated :) Thanks,

Alberto

Pascal Costanza

unread,
Sep 14, 2012, 4:35:30 PM9/14/12
to
It's possible to do this. This will probably not have any serious
performance impacts. It could have a performance impact if you create
new classes a lot, at a high frequency, but otherwise it shouldn't.

You should probably make an effort to cache class combinations, because
CLOS won't realize that a particular class with the same direct
superclasses was already created before.


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
The views expressed are my own, and not those of my employer.

Pixie

unread,
Sep 14, 2012, 11:24:06 PM9/14/12
to
You may be interested in metabang's Dynamic-Classes[1], which is a
library for doing the create-a-class-of-assorted-mixins-at-runtime kind
of thing.


[1] http://common-lisp.net/project/dynamic-classes/

kent...@gmail.com

unread,
Sep 23, 2012, 2:47:18 AM9/23/12
to
You have neglected to notice that you are creating a DSL. The problem you are trying to solve tried to point this out to you when it said you would need to dynamically construct CLOS classes from other CLOS classes. Those who would help you do so will only block the problem's message. The problem will then have to find other ways to send you the same message in the form of other coding challenges you will fight thru with this group's non-assisting assistance.

So stop asking CLOS to do so much. Start consciously working on the DSL. CLOS can help you implement the DSL. The key is to separate in your mind and then the design that which is DSL and that which is implementation.

hth,hk

CLOS is for static programming. Just Stop Using It. Or, use it to implement

kent...@gmail.com

unread,
Sep 23, 2012, 2:50:04 AM9/23/12
to
Back in the day some guy from Northwestern (I think) offered a library called Capabilities. Haven't been able to google it up in years.

-kt

Pascal J. Bourguignon

unread,
Sep 23, 2012, 11:04:47 AM9/23/12
to
kent...@gmail.com writes:

> CLOS is for static programming. Just Stop Using It. Or, use it to implement

MOP is for dynamic programming. You may start using it.

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Dimitri Fontaine

unread,
Dec 10, 2012, 3:26:38 PM12/10/12
to
Alberto Riva <a...@nospam.ufl.edu> writes:
> For example, P could indicate an analysis method, and Q could be used to
> specify the type of output desired (text, html, etc). The specific pair of P
> and Q classes is determined at runtime based on user input.

It looks like multiple dispatch of methods is what you're looking for, right?

Regards,
--
dim

Dimitri Fontaine

unread,
Dec 10, 2012, 3:28:05 PM12/10/12
to
Alberto Riva <a...@nospam.ufl.edu> writes:
> For example, P could indicate an analysis method, and Q could be used to
> specify the type of output desired (text, html, etc). The specific pair of P
> and Q classes is determined at runtime based on user input.

Marco Antoniotti

unread,
Dec 11, 2012, 1:04:23 AM12/11/12
to
I am pretty sure Alberto knows of multiple dispatching :) But yes. Maybe the original problem could be more easily worked out with m-d. I just wonder why not. OTOH, the trick og generating classes at runtime using the MOP is interesting.

Cheers
--
MA

tar...@google.com

unread,
Dec 11, 2012, 7:42:27 PM12/11/12
to
One other option would be to use Loom, a lisp-based knowledge representation system. It allows you to create instances that belong to more than one concept. There is even an option that allows this with (more or less) regular CLOS instances.

But depending on what one wanted to do with the resulting instances, this may be an option.

See http://www.isi.edu/isd/LOOM

For this particular use, Loom would probably be a better choice than PowerLoom.

-Tom.
Disclaimer: I was one of the Loom developers.

Raymond Wiker

unread,
Dec 12, 2012, 12:19:29 PM12/12/12
to
I did something like that a while back, just out of
curiosity... A colleague had shared a github project for reading a legacy
xml format, in Java, and I wanted to try run-time class definition on
this problem.

I never tried to run his original code (as I didn't want to
install Eclipse just for that), but... his code ran to 3767 lines; mine
was less than 100 lines of Lisp. Of those, 30 were for functionality
that would be useful outside that particular setting (basically, a
defmethod for slot-missing that would add slots to the class as
required, plus support code.)
0 new messages