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

Hans Hübner hans.huebner at gmail.com
Tue Apr 23 00:49:31 PDT 2013

On Mon, Apr 22, 2013 at 7:32 PM, Ron Garret <ron at flownet.com> wrote:

> 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.

You are asking for "better/easier" ways, so here are some options:

- Have your users inherit from a base class that provides the default
primary key slot, use a slot option and the MOP to give  users the ability
to designate a different slot for the primary key (Pascal pointed that
out).  The MOP will then mostly used to allow the extra options.
- If you dislike the requirement for an explicit common base class, modify
the class inheritance list of classes using your metaclass to include the
common base class holding the primary key slot.  This is not quite as
straightforward as it sounds because you'll have to find out whether the
primary key holding base class is already in the ancestor list of a class
before you add it.
- You can also avoid storing the artificial primary key in the user's
instances altogether by putting it into a (weak) hash table keyed by the
instance identity.  This may actually be the "easiest" option, although you
stated the requirement that some classes would use another slot as their
primary key.

For the first two approaches, maybe
https://github.com/slyrus/xml-class/blob/master/xml-class.lisp gives you
some implementation hints.

"Adding a slot dynamically" neither sounds like the "best" nor the
"easiest" option.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20130423/950e70db/attachment.htm>

More information about the Openmcl-devel mailing list