[racket-dev] [racket] Question about fields in Racket OO

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Dec 16 09:20:40 EST 2010

At Thu, 16 Dec 2010 07:08:11 -0700, Jay McCarthy wrote:
> This seems like a trivial point because the class system doesn't have to
> track these things and they are in fact part of the closures of the methods,

Not directly. They are accessible in methods via an implicit `this'.

> so I don't see in what sense they are "fields".

The example

 (define o (new (class object%
                   (define x (gensym)))))

is analogous to

 (define o (let ()
            (struct s (x))
            (s (gensym))))

In both cases, the `x' field is unused and inaccessible, but the
generated symbol is retained as long as `o' is reachable. 

Both of those are different than

 (define o (let ()
             (define x (gensym))
             (lambda () 10)))

where `x' could have been referenced by the `lambda' form, but since it
isn't, nothing retains `x'.

You could image trying to implement private fields as local bindings
that are captured by method closures. That means, though, that you have
to create separate method closures for each object --- which is the bad
old class system.

Posted on the dev mailing list.