Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- From: Peter Seibel <peter@xxxxxxxxxxxxxxx>
- Date: Fri, 30 Sep 2005 20:12:16 GMT
Pascal Costanza <pc@xxxxxxxxx> writes:
> 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.
Though he will have to use SLOT-VALUE, i.e.
(setf (slot-value obj 'name) "Barney")
rather than:
(setf (name obj) "Barney")
or (what he actually said he wanted):
(setf (obj name) "Barney")
I don't think there's anything that's going to let SETF do what he
wants in the latter case. However perhaps with Allegro's compile-time
environments one could implement one's own assignment macro:
(defmacro my-set (place value)
(if (and (consp place) (= (length place) 2) (env:variable-p (first place)))
`(setf (gethash ',(second place) ,(first place)) ,value)
`(setf ,place ,value)))
where ENV:VARIABLE-P is a made up function that tells you whether a
symbol is the name of a variable in the current scope. (I'm assuming
that if it is, that it is the name of an object--one might want to
check first that it's not the name of a SETF'able function, another
thing that I'm assuming there'd be some way to determine that with the
environment access foo.)
Perhaps Duanne can chime in as to whether this is actually possible in
Allegro.
-Peter
--
Peter Seibel * peter@xxxxxxxxxxxxxxx
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/
.
- References:
- The limits of lisps synatactic gymnastics? (i sure hope not)
- From: robbie.carlton@xxxxxxxxx
- Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- From: Pascal Costanza
- The limits of lisps synatactic gymnastics? (i sure hope not)
- Prev by Date: Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- Next by Date: Re: The limits of lisps synatactic gymnastics? (i sure hope not)
- Previous by thread: Re: 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):