[plt-scheme] levels of access to object fields
At Thu, 18 Jan 2007 06:33:49 -0500, Prabhakar Ragde wrote:
> Is it possible to make a field of an object that is accessible to
> objects of the same class but otherwise private? I think I can do this
> in awkward ways (e.g. defining a struct within the class and wrapping
> the field in it). Is there a clean way? Thanks. --PR
Use `define-local-member-name':
(define c%
(let ()
(define-local-member-name m) ; confines `m' to inside the `let'
(class object%
(init-field x)
(define/public (m) x)
(define/public (get-other o) (send o m))
(super-new))))
(define a (new c% [x 1]))
(define b (new c% [x 2]))
(send a get-other b) ; => 2
(send b m) ; => no such method "m"
Matthew