[closer-devel] interesting contextl use-case
Pascal Costanza
pc at p-cos.net
Tue Jun 12 18:44:28 UTC 2007
On 12 Jun 2007, at 20:26, Attila Lendvai wrote:
>> I don't fully understand what you are asking for, but let me make
>> some guesses. ;)
>
> yes, you did! :)
:)
> i would be very grateful if you could give me some links/hints on
> how the hashtable based slots work! i always thought that it's a
> deficiency of CLOS that the slot accessor atom (namely
> SLOT-VALUE-USING-CLASS) is dispatching on the effective slot instance,
> so there's no way to create a CLOS metaclass that has a working
> CL:SLOT-VALUE and can have a variable number of slots stored in a
> hashtable; e.g. the set of slot names not know at definition time.
The examples in AMOP indeed go through slot-value-using-class, but
the CLOS MOP was changed in the last minute before publication of the
book to dispatch on slot definition metaobjects instead of slot
names. So doing this via slot-value-using-class has indeed limitations.
The trick is to go through slot-missing instead. You don't even need
your own metaclass, a simple mixin class is sufficient.
Here is some rough code that shows how to do it:
(defclass hash-slots-object ()
((hash-slots :accessor hash-slots
:initform (make-hash-table :test #'eq))))
(defmethod slot-missing ((class t)
(object hash-slots-object)
slot-name operation &optional new-value)
(ecase operation
(slot-value (multiple-value-bind (value present-p)
(gethash slot-name (hash-slots object))
(if present-p value
(slot-unbound class object slot-name))))
(setf (setf (gethash slot-name (hash-slots object))
new-value))
(slot-boundp (nth-value 1 (gethash slot-name (hash-slots
object))))
(slot-makunbound (remhash slot-name (hash-slots object)))))
slot-missing is part of ANSI Common Lisp, so this is fully portable
code.
I hope this helps.
Pascal
--
Pascal Costanza, mailto:pc at p-cos.net, http://p-cos.net
Vrije Universiteit Brussel, Programming Technology Lab
Pleinlaan 2, B-1050 Brussel, Belgium
More information about the closer-devel
mailing list