What is Rackets canonical way to define fields called in other languages "static", which means the value is shared among all instances of the class. I can use a closure, but I can not access the value without an object:
#lang racket
(define parent%
(let ((i 0))
(class object%
(super-new)
(define/public (inc)
(set! i (+ i 1)))
(define/public (get)
i))))
(define child%
(class parent%
(super-new)
(inherit inc)))
(define p (new parent%))
(define c1 (new child%))
(define c2 (new child%))
(send p inc)
(send c1 inc)
(send c2 inc)
(send p get)
If you want to be able to access the variable without going through an object, you could create additional function(s) for doing so inside the closure:
(define-values (parent% peek)
(let ((i 0))
(values
(class object%
(super-new)
(define/public (inc)
(set! i (+ i 1)))
(define/public (get)
i))
(lambda () i))))
(define child%
(class parent%
(super-new)
(inherit inc)))
(define p (new parent%))
(define c1 (new child%))
(define c2 (new child%))
(send p inc)
(send c1 inc)
(send c2 inc)
(peek)
Another approach would be module-level variable or parameter that is defined in the same module as the class. I admit it’s not super OOPy but I’m having trouble imagining why anything more elaborate would be desirable.