<div dir="ltr">On Mon, Apr 22, 2013 at 7:32 PM, Ron Garret <span dir="ltr"><<a href="mailto:ron@flownet.com" target="_blank">ron@flownet.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

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

</blockquote><div><br></div><div style>You are asking for "better/easier" ways, so here are some options:<br><br></div><div style>- 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.</div>

<div style>- 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.</div>

<div style>- 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.</div>

<div style><br></div><div style>For the first two approaches, maybe <a href="https://github.com/slyrus/xml-class/blob/master/xml-class.lisp">https://github.com/slyrus/xml-class/blob/master/xml-class.lisp</a> gives you some implementation hints.</div>

<div style><br></div><div style>"Adding a slot dynamically" neither sounds like the "best" nor the "easiest" option.</div><div style><br></div><div style>-Hans</div></div></div></div>