In Racket, how to get and set the field of an object as if it were a local variable?

advertisements

I want to do something like this:

(define-field-magic color car)
(set! color "green")
(pretty-display color)

Instead of this:

(set-field! color car "green")
(pretty-display (get-field color car))


One solution, adapted from the The Racket Guide, is:

(define-syntax color
  (syntax-id-rules (set!)
    [(set! color e) (set-field! color car e)]
    [(clock a ...) ((get-field color car) a ...)]
    [clock (get-field color car)]))

But there might be better and more readable ways.