[Openmcl-devel] [off-topic] Adding a slot to a class using the MOP

Pascal Costanza pc at p-cos.net
Mon Apr 22 23:32:49 PDT 2013

You're invoking undefined behavior here, because your method on ensure-class-using-class doesn't specialize on any of your own metaclasses. See bullet one in "Restrictions on Portable Programs" in the CLOS MOP specification.


On 23 Apr 2013, at 08:29, Ron Garret <ron at flownet.com> wrote:

> Why not this?
> (defmethod ensure-class-using-class :before (class name &rest args)
>  (setf (get 'class-info name) (list* class args)))
> (defun add-slot (class-name slotspec)
>  (destructuring-bind (class . args) (get 'class-info class-name)
>    (push slotspec (getf args :direct-slots args))
>    (apply 'ensure-class-using-class class class-name args)))
> It seems to work:
> ? (defclass foo () (x y z))
> ? (add-slot 'foo '(:name q))
> ? (describe (make-instance 'foo))
> #<FOO #x3020028B7DAD>
> Wrapper: #<CCL::CLASS-WRAPPER FOO #x3020028B9EBD>
> Instance slots
> Q: #<Unbound>
> X: #<Unbound>
> Y: #<Unbound>
> Z: #<Unbound>
> On Apr 22, 2013, at 1:23 PM, Pascal Costanza wrote:
>> Check out AspectL, it provides exactly what you want.
>> However, it's also worthwhile spending some time on thinking about whether there is a better design for your problem. Adding slots like this creates a lot of problems. Maybe a typical hash table based meta class already provides what you need...
>> Pascal
>> Sent from my iPad
>> On 22 Apr 2013, at 19:32, Ron Garret <ron at flownet.com> wrote:
>>> Apologies for the non-CCL-related question, but c.l.l. is slow today and I'm really stuck on this.
>>> I need to dynamically add a slot to a class using the MOP.  There doesn't seem to be a standard add-slot method, so I figure I need to use something like ensure-class.  To do that, I need to reconstruct the argument to :direct-slots from the existing class definition so I can add a slot without disturbing the existing slots.  Before I went down this rabbit hole I thought I'd ask: is there a better/easier way to do this?  Surely I'm not the first person to want to do this.
>>> Many thanks,
>>> rg
>>> _______________________________________________
>>> Openmcl-devel mailing list
>>> Openmcl-devel at clozure.com
>>> http://clozure.com/mailman/listinfo/openmcl-devel

Pascal Costanza

More information about the Openmcl-devel mailing list