Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Fri, 30 Sep 2005 21:30:52 +0200
robbie.carlton@xxxxxxxxx wrote:
Hi. I'm trying to create a simple alternative object system for common lisp. Whilst I admire the power and generality of clos, it's a little unwieldy for some little things. What I'm looking for is something more in the model of ruby/javascript/python, where an object is basically a hash table of properties with very simple accessing syntax. A very important requirement for me is that setfing an undefined property defines it, so assignment and property initialisation look the same.
CLOS is so general that it can even give you the simple approaches. Whenever a slot is not defined for a class, an access to such a slot issues a "slot is missing" error. However, there is a defined way to intercept that error message and something useful instead, like doing a lookup in a hashtable.
Here is the implementation:
(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)))))Just use hash-slots-object as a mixin, and you can do what you want.
Pascal
-- OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol ++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++ .
- Follow-Ups:
- Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- From: Peter Seibel
- Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- References:
- The limits of lisps synatactic gymnastics? (i sure hope not)
- From: robbie.carlton@xxxxxxxxx
- The limits of lisps synatactic gymnastics? (i sure hope not)
- Prev by Date: The limits of lisps synatactic gymnastics? (i sure hope not)
- Next by Date: Re: Giving info to CONSTANTP
- Previous by thread: The limits of lisps synatactic gymnastics? (i sure hope not)
- Next by thread: Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- Index(es):