[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

How to enforce metaclass on subclasses

Jim Newton

8/24/2015 9:47:00 AM

I have defined a class named HAS-CALL which has an application specific metaclass named APP-METACLASS.

I would like all subclasses of HAS-CALL to also have the same meta-class, or potentially a subclass thereof.

The only way I can think of doing this is write a macro around defclass, or simply just always remember.

(defclass U (has-call)
()
(:metaclass app-metaclass))

(defclass V (U)
()
(:metaclass app-metaclass))

(defclass W (V)
()
(:metaclass app-metaclass))

If I forget, and define W as follows, I get the error shown below.
(defclass W (V)
())

The class #<APP-METACLASS V> was specified as a superclass of
the class #<STANDARD-CLASS W {101EE831D3}>, but the metaclasses
#<STANDARD-CLASS APP-METACLASS> and
#<STANDARD-CLASS STANDARD-CLASS> are incompatible.
[Condition of type SB-PCL::INVALID-SUPERCLASS]
See also:
The Art of the Metaobject Protocol, VALIDATE-SUPERCLASS [:generic-function]


Isn't there a way enhance either HAS-CALL or APP-METACLASS to enforce this automatically?
I've looked into ensure-class-using-class, but it is not clear to me how to make it work.

4 Answers

Jim Newton

8/25/2015 9:47:00 AM

0

Actually, the more I think about this, the more I doubt a good solution exists. Why? Because given the defclass form, the class does not yet exist, so nothing can dispatch on it.

Didier Verna

8/25/2015 2:58:00 PM

0

Jim Newton wrote:

> I have defined a class named HAS-CALL which has an application
> specific metaclass named APP-METACLASS. I would like all subclasses
> of HAS-CALL to also have the same meta-class, or potentially a
> subclass thereof.

> The only way I can think of doing this is write a macro around
> defclass, or simply just always remember. Isn't there a way enhance
> either HAS-CALL or APP-METACLASS to enforce this automatically? I've
> looked into ensure-class-using-class, but it is not clear to me how to
> make it work.

IIUYC, every class based on APP-METACLASS would enforce the same
meta-class on every subclasses. So ENSURE-CLASS-USING-CLASS would need
to look at the direct superclasses (that should be enough) and add a
:metaclass to the call if missing.

But this sounds too much like a DWIM thing to do to me (what about
multiple inheritance for example?) so I would stick to a defclass
wrapper.

--
My new Jazz CD entitled "Roots and Leaves" is out!
Check it out: http://didierverna.com/records/roots-and-...

Lisp, Jazz, Aïkido: http://www.didier...

Jim Newton

8/25/2015 4:01:00 PM

0

A problem I see it, with using ENSURE-CLASS-USING-CLASS is there is nothing for it to specialise on. If it specialises on APP-METACLASS it won't be called.

(defmethod ENSURE-CLASS-USING-CLASS ((class APP-METACLASS) name &rest args &key metaclass)
...)

If the metaclass argument IS app-metaclass, then everything is fine,
but if app-metaclass IS NOT the metaclass argument, then class is not a subclass of app-metaclass, thus this method won't be applicable.

Blake McBride

8/28/2015 1:35:00 PM

0

On 08/24/2015 04:46 AM, Jim Newton wrote:
> I have defined a class named HAS-CALL which has an application specific metaclass named APP-METACLASS.
>
> I would like all subclasses of HAS-CALL to also have the same meta-class, or potentially a subclass thereof.
>
> The only way I can think of doing this is write a macro around defclass, or simply just always remember.
>
> (defclass U (has-call)
> ()
> (:metaclass app-metaclass))
>
> (defclass V (U)
> ()
> (:metaclass app-metaclass))
>
> (defclass W (V)
> ()
> (:metaclass app-metaclass))
>
> If I forget, and define W as follows, I get the error shown below.
> (defclass W (V)
> ())
>
> The class #<APP-METACLASS V> was specified as a superclass of
> the class #<STANDARD-CLASS W {101EE831D3}>, but the metaclasses
> #<STANDARD-CLASS APP-METACLASS> and
> #<STANDARD-CLASS STANDARD-CLASS> are incompatible.
> [Condition of type SB-PCL::INVALID-SUPERCLASS]
> See also:
> The Art of the Metaobject Protocol, VALIDATE-SUPERCLASS [:generic-function]
>
>
> Isn't there a way enhance either HAS-CALL or APP-METACLASS to enforce this automatically?
> I've looked into ensure-class-using-class, but it is not clear to me how to make it work.
>

When I started with CLOS, I wanted the metaclass/class hierarchy like
Smalltalk (except with multiple inheritance). I think Smalltalk got it
right for the bulk of the cases. CLOS is more primitive but gives you
all of the building blocks you need. See my notes at:

http://admin.arahant.com/wiki/Wiki.jsp...

I am attaching the code that actually implements this herewith.

Hope this helps.

Blake McBride