Re: Specializing slot-value-using-class in OpenMCL
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Wed, 30 May 2007 21:18:17 +0200
Slava Akhmechet wrote:
Pascal Costanza <pc@xxxxxxxxx> writes:
(defmethod slot-value-using-classAh, I see. Does this work with standard-class? I seem to be getting
((class mymetaclass) object slotd)
...)
inconsistant results (works in a simple testcase but not in my
codebase). Am I required to specify a custom metaclass? If this is the
case, could you give a very simple example of how to define a class
with custom metaclass? I don't have the AMOP book and there seems to
be almost no information on this online.
You are not allowed to change the behavior of predefined metaclasses (and also not that of predefined classes, by the way). Otherwise a CLOS implementation would have a very hard time to optimize its internal default behavior.
The idiom to introduce your own metaclass is this:
(defclass mymetaclass (standard-class)
())
(defmethod validate-superclass
((class mymetaclass) (superclass standard-class))
"this is necessary so that you can inherit from other classes"
t)
(defmethod slot-value-using-class
((class mymetaclass) object slot)
...)
....same for the other slot-xyz-using-class functions.
It is sometimes useful to distinguish between default slot access and customized slot access even in your own metaclasses. For that, you can introduce new slot definition classes:
(defclass my-effective-slot-definition
(standard-effective-slot-definition)
())
(defmethod effective-slot-definition-class
((class mymetaclass) &rest initargs)
(if ... some condition ...
(find-class 'my-effective-slot-definition)
(find-class 'standard-effective-slot-definition)))
....and then specialize slot-value-using-class additionally on the new slot definition metaclass:
(defmethod slot-value-using-class
((class mymetaclass) object (slot my-effective-slot-definition))
...)
I hope this helps. You can find the CLOS MOP specification at http://www.lisp.org/mop/
Cheers,
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/
.
- Follow-Ups:
- Re: Specializing slot-value-using-class in OpenMCL
- From: Slava Akhmechet
- Re: Specializing slot-value-using-class in OpenMCL
- References:
- Specializing slot-value-using-class in OpenMCL
- From: Slava Akhmechet
- Re: Specializing slot-value-using-class in OpenMCL
- From: Pascal Costanza
- Re: Specializing slot-value-using-class in OpenMCL
- From: Slava Akhmechet
- Specializing slot-value-using-class in OpenMCL
- Prev by Date: redefining SBCL prompts
- Next by Date: Re: Please check this bit of Lisp (how ugly is my code?)
- Previous by thread: Re: Specializing slot-value-using-class in OpenMCL
- Next by thread: Re: Specializing slot-value-using-class in OpenMCL
- Index(es):