Hi,
I have a class that is an instance of a specified metaclass. When I create
subclasses of this class I would like them to be of the same metaclass without
saying so explicitly every time. For example I define a metaclass meta like this:
(defclass meta (standard-class) ())
#<STANDARD-CLASS META>
Then I create class a:
(defclass a () () (:metaclass meta))
#<META A>
A subclass of class a is by default an instance of standard-class:
(defclass b (a) ())
#<STANDARD-CLASS B>
Instead, I would like it to be an instance of class meta. I know I could give it
the :metaclass keyword as I did in the class definition of a. But is there a way
I can set a default metaclass for all subclasses of a?
Alexandra
Alexandra Kirsch wrote:
> Hi,
>
> I have a class that is an instance of a specified metaclass. When I
> create subclasses of this class I would like them to be of the same
> metaclass without saying so explicitly every time. For example I define
> a metaclass meta like this:
>
> (defclass meta (standard-class) ())
> #<STANDARD-CLASS META>
>
> Then I create class a:
> (defclass a () () (:metaclass meta))
> #<META A>
>
> A subclass of class a is by default an instance of standard-class:
> (defclass b (a) ())
> #<STANDARD-CLASS B>
>
> Instead, I would like it to be an instance of class meta. I know I could
> give it the :metaclass keyword as I did in the class definition of a.
> But is there a way I can set a default metaclass for all subclasses of a?
No. See
http://groups.google.com/group/comp.lang.clos/browse_thread/thread/d7c9934debf719b0/4f44f61ebe888602?hl=en#4f44f61ebe888602
You may have a chance to use change-class on your class instances before
finalize-inheritance is first called, for example in a before method
on finalize-inheritance or in a method on initialize-instance - but
you'd be swimming in dangerous waters, because AMOP doesn't allow this.
(The idea is discussed in a paper about ECLOS in Zimmermann (ed.),
Advances in Object-Oriented Metalevel Architectures and Reflection.)
Another workaround is to shadow defclass and let a new defclass macro
expand accordingly for your own code, like this:
(shadow 'defclass) ; better do this in a defpackage definition
(defmacro defclass (&whole form
name (&rest superclasses)
(&rest slots)
&body options)
(if (assoc :metaclass options)
form
`(cl:defclass ,name ,superclasses ,slots ,@options
(:metaclass my-metaclass))))
(This is untested.)
Pascal
--
In computer science, we stand on each other's feet. - Brian K. Reid
Pascal Costanza wrote:
> Another workaround is to shadow defclass and let a new defclass macro
> expand accordingly for your own code, like this:
>
> (shadow 'defclass) ; better do this in a defpackage definition
>
> (defmacro defclass (&whole form
> name (&rest superclasses)
> (&rest slots)
> &body options)
> (if (assoc :metaclass options)
> form
> `(cl:defclass ,name ,superclasses ,slots ,@options
> (:metaclass my-metaclass))))
>
> (This is untested.)
...and indeed it is buggy. The &whole form hack would cause this macro
to run into an endless loop. The first case should also expand into
`(cl:defclass ...) but should omit the :metaclass option.
Pascal
--
In computer science, we stand on each other's feet. - Brian K. Reid
"Alexandra Kirsch" <······@in.tum.de> schrieb im Newsbeitrag
···················@sunsystem5.informatik.tu-muenchen.de...
> Hi,
>
> I have a class that is an instance of a specified metaclass. When I create
> subclasses of this class I would like them to be of the same metaclass
without
> saying so explicitly every time. For example I define a metaclass meta
like this:
>
> (defclass meta (standard-class) ())
> #<STANDARD-CLASS META>
>
> Then I create class a:
> (defclass a () () (:metaclass meta))
> #<META A>
>
> A subclass of class a is by default an instance of standard-class:
> (defclass b (a) ())
> #<STANDARD-CLASS B>
>
> Instead, I would like it to be an instance of class meta. I know I could
give it
> the :metaclass keyword as I did in the class definition of a. But is there
a way
> I can set a default metaclass for all subclasses of a?
>
> Alexandra
Alexandra,
no, I don't think. Writers of metaclasses may want to make them compatible
to other metaclasses. CLOS makes this possible.
You can write a tiny macro (ie. DEF-CLASS) which wraps around DEFCLASS and
simply inserts your metaclass into the statement.
Andreas