From: rif
Subject: CLOS/MOP question
Date: 
Message-ID: <wj0lldn391q.fsf@geskekelud.mit.edu>
Abstraction of my problem: I have a hierarchy of classes.  For any
object in this hierarchy, it is meaningful (the semantics are
well-defined) to combine two objects into a new object (whose class
will be the LCA of the two classes in the hierarchy).  Every slot in
the class can be either a "foo" slot or a "not-foo" slot --- if it's a
"foo" slot, then I combine the appropriate slots of the two objects in
a certain way, if it's a "not foo" slot, then I do it in a different
way.

What are good ways to accomplish this using CLOS/MOP?  I'm using
CMUCL, and I'd prefer it to be as portable as possible, but I'm just
considering options at this point.  I can certainly imagine ways to do
what I want by going outside of CLOS --- basically defining my own
macros around defclass to keep track of things, but I'm wondering if
this is a reasonable CLOS/MOP task.

My initial idea was to subclass standard-direct-slot-definition (and
similarly standard-effective-slot-definition) to make
foo-or-not-foo-slot-definition, which I can certainly do, but then I'm
stuck --- I imagine I'd need to subclass standard-class to
foo-or-not-foo-standard-class in such a way that all the slots in
objects subclassed from this were foo-or-not-foo-slots (and then
create my classes with the :metaclass option), but I don't know how to
do that.  Any suggestions how to do that, or anything else relevant?

I have the Art of the MOP, but I'm not immediately finding what I'm
looking for in there.  I suppose I should just read the whole book,
but I don't quite have the time right now...

Cheers,

rif

From: Rahul Jain
Subject: Re: CLOS/MOP question
Date: 
Message-ID: <87oeij8tam.fsf@nyct.net>
rif <···@mit.edu> writes:

> My initial idea was to subclass standard-direct-slot-definition (and
> similarly standard-effective-slot-definition) to make
> foo-or-not-foo-slot-definition, which I can certainly do, but then I'm
> stuck --- I imagine I'd need to subclass standard-class to
> foo-or-not-foo-standard-class in such a way that all the slots in
> objects subclassed from this were foo-or-not-foo-slots (and then
> create my classes with the :metaclass option), but I don't know how to
> do that.  Any suggestions how to do that, or anything else relevant?

Override EFFECTIVE-SLOT-DEFINITION-CLASS and
DIRECT-SLOT-DEFINITION-CLASS.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Steven M. Haflich
Subject: Re: CLOS/MOP question
Date: 
Message-ID: <hxfhd.15965$6q2.11959@newssvr14.news.prodigy.com>
rif wrote:
> Abstraction of my problem: I have a hierarchy of classes.  For any
> object in this hierarchy, it is meaningful (the semantics are
> well-defined) to combine two objects into a new object (whose class
> will be the LCA of the two classes in the hierarchy).

Unless you can impose additional constraints on these classes, it
won't always be well defined to combine arbitrary classes.  This
example shows why:

cl-user(2): (defclass foo (a b) ())
#<standard-class foo>
cl-user(3): (defclass bar (b a) ())
#<standard-class bar>
cl-user(4): (defclass goo (foo bar) ())
#<standard-class goo>
cl-user(5): (defclass a () ())
#<standard-class a>
cl-user(6): (defclass b () ())
#<standard-class b>
cl-user(7): (mop:finalize-inheritance (find-class 'goo))
Error: While computing the class precedence list of the class named goo.
It is not possible to compute the class precedence list because
there is a circularity in the local precedence relations.
This arises because:
   the class named a follows the class named b in the supers of the class named bar
   the class named b follows the class named a in the supers of the class named foo.
   [condition type: program-error]

But, of course, class precedence partial ordering might not be an
issue in your intended use.
From: Bruno Haible
Subject: Re: CLOS/MOP question
Date: 
Message-ID: <cm5i8v$61j$1@laposte.ilog.fr>
> My initial idea was to subclass standard-direct-slot-definition (and
> similarly standard-effective-slot-definition) to make
> foo-or-not-foo-slot-definition, which I can certainly do, but then I'm
> stuck --- I imagine I'd need to subclass standard-class to
> foo-or-not-foo-standard-class in such a way that all the slots in
> objects subclassed from this were foo-or-not-foo-slots (and then
> create my classes with the :metaclass option)

Yes, you need to have a subclass of STANDARD-CLASS, because all the
MOP customizations that make sense here (such as
DIRECT-SLOT-DEFINITION-CLASS or EFFECTIVE-SLOT-DEFINITION-CLASS) are
based on the metaclass, and you are not allowed to replace the
original methods specialized on STANDARD-CLASS itself.

Whether in this subclass you support only foo-or-not-foo-slots or also
normal slots, is up to you.

> I have the Art of the MOP, but I'm not immediately finding what I'm
> looking for in there.

You can take a look at the examples in CLISP's testsuite [1],
especially the auto-accessors-class, external-slot-definition and
typechecked-slot-definition examples.

              Bruno


[1] http://cvs.sourceforge.net/viewcvs.py/clisp/clisp/tests/mop.tst?rev=1.17&view=text