[plt-scheme] Swindle get-slot and set-slot methods?
On Jan 16, Will M. Farr wrote:
> Hello all,
>
> I'm curious if there's a way to have get-slot and set-slot methods in
> swindle, as in the following example (this is very useful for wrapping
> up an object which has an unusual data representation in a class which
> provides a more standard way of accessing the underlying data):
>
> (defclass <complex> ()
> x y (r :get [...] :set [...]) (theta :get [...] :set [...]))
>
> In this example, (slot-ref z 'r) would use the :get method, (slot-set!
> z 'theta new-theta) would use the :set method, etc. Of course, you can
> do this if you only access the "fake" slots via the generic functions
> (complex-r ...) and company, but it would be neat if this could be made
> to work with the (with-slots ...) macros and (slot-ref ...). Is there
> a known way to do this cleanly?
No -- `with-slots' uses `slot-ref', which is a plain function. I
thought about making it a generic, but that will be way too expensive
for something that is rarely useful. Using getters and setters is a
much better solution. The CLOS-like solution for this would be to
implement `slot-missing', which gives you a hook for playing such
tricks.
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!