Monday, April 28, 2008

‘Getting’ CLOS

Eric asked me: I have still yet to really "get"/appreciate CLOS. Any tips on reaching this end?

I'm not sure I'm exactly the right person to ask. It took me years to ‘get’ CLOS. It just rubbed me the wrong way and seemed to be gratuitously verbose and complex. I think it ‘clicked’ for me when I got painted into a corner as I was trying to update the schema of some persistent objects. I discovered that I could rename the slot of the legacy object without renaming the accessors and arrange for the accessors to act differently on legacy objects versus new ones. It was completely transparent, almost as if it were designed for this purpose, which it was.

I guess my advice would be this. Keep in mind that CLOS objects are ‘structs on steroids’. Whenever you would normally think of using a struct, use a class. Use the high-level accessors that DEFCLASS defines. Avoid using SLOT-VALUE or WITH-SLOTS (these are low-level accessors). Avoid using fancy method combination. Anyplace you think you need an :AROUND method, you probably just need to invoke CALL-NEXT-METHOD in a normal method.

I should probably write up some guidelines, but that'll have to wait for another day.
Post a Comment