[plt-scheme] Swindle get-slot and set-slot methods?

From: Will M. Farr (farr at MIT.EDU)
Date: Mon Jan 16 15:35:54 EST 2006

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 (lambda (z)
             (with-slots z (x y)
               (sqrt (+ (square x) (square y)))))
      :set (lambda (z new-r)
             (with-slots z (x y theta)
               (set! x (* new-r (cos theta)))
               (set! y (* new-r (sin theta))))))
   (theta :get (lambda (z)
                 (with-slots z (x y)
                   (if (= x 0)
                         ((= y 0) 0)
                         ((> y 0) (/ pi 2))
                         (else (- (/ pi 2)))))))
          :set (lambda (z new-theta)
                 (with-slots z (x y r)
                   (set! x (* r (cos new-theta)))
                   (set! y (* r (sin new-theta)))))))

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?

Thanks for the help,

Posted on the users mailing list.