opening it up with Common Lisp

Favorite weblogs

Lisp Related

Bill Clementson

Finding Lisp

Lemonodor

Lispmeister.com

Planet Lisp

Politics

Orcinus

Talking Points Memo

This Modern World

Working for Change

Other home

Polliblog

Recent Readings

Book review: Darwinia
Reviewed: Friday, August 11, 2006

Summer reading: Spin
Reviewed: Saturday, August 5, 2006

Runner
Reviewed: Tuesday, July 18, 2006

the Omnivoire's Delimma
Reviewed: Wednesday, July 12, 2006

the Golem's Eye
Reviewed: Wednesday, May 31, 2006





tinderbox
 width=

Why change-class?
Thursday, June 16, 2005

(updated 21 June 2005)

Common Lisp lets you change the class of things on the fly and in fact has a whole protocol for dealing with appearing and disappearing slots, updating existing instances and so forth. It's really quite amazing and very handy for prototyping and ad hoc experimentation. My only complaint is that changing the class of an object takes way too much typing! You need to

(change-class my-object (find-class 'name-of-class))

This seems silly to me. Why not make class changing as simple as setting any other sort of value? Why not make it seem like Common Lisp by using setf? Why not make it more flexible? So, since I couldn't think of any not to, I wrote this:


(defgeneric (setf class) (class object)
  (:documentation "")
  (:method ((class symbol) (object standard-object))
           (change-class object (find-class class)))
  (:method ((class standard-object) (object standard-object))
           (setf (class object) (class-of class)))
  (:method ((class standard-class) (object standard-object))
           (change-class object class)))

This lets me do things like

(setf (class my-object) 'a-class-name)

and that's easier to type, looks like CL, and is even clearer (in my opinion). Any dissenting voices are welcome to comment!

Comments

Well, it looks like I've put my foot in it! Not only can you already say things like

(change-class my-object 'a-class-name)

but redefining a built in symbol of the Common Lisp package like class is -- of course -- a no-no. My implementation also ignores the fact that you might want to add initargs to the change-class form (though that's easy to fix). On the other hand, I still think that using setf to change classes is a good idea. My general philosophy of change in Lisp is that if you can use setf, then you should. In any case, thanks for the feedback.


|

Home | About | Quotes | Recent | Archives

Copyright -- Gary Warren King, 2004 - 2006