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

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Thu Dec 16 09:09:16 EST 2010

There is one field per object, but one method (closure) per class.
Otherwise, you're right; and that's just what fields are. :)

Robby

On Thu, Dec 16, 2010 at 8:08 AM, Jay McCarthy <jay.mccarthy at gmail.com> 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,
> so I don't see in what sense they are "fields". Perhaps I am blinded by my
> reading of the implementation. I certainly agree they are essentially
> fields, but I can't but think of them as closed-over variables.
> Jay
>
> On Thu, Dec 16, 2010 at 7:00 AM, Carl Eastlund <cce at ccs.neu.edu> wrote:
>>
>> To quote the class* documentation:
>>
>> (http://docs.racket-lang.org/reference/createclass.html#%28part._clfields%29)
>>
>> "Each field, init-field, and non-method define-values clause in a
>> class declares one or more new fields for the class. Fields declared
>> with field or init-field are public."
>>
>> So only the public ones are accessible via get-field.
>>
>> Carl Eastlund
>>
>> On Thu, Dec 16, 2010 at 8:56 AM, Jay McCarthy <jay.mccarthy at gmail.com>
>> wrote:
>> > Does 'define' really mean 'make a field'? I thought fields had to be
>> > specially designated so that get-field would know about them...
>> > Yes, this program errors:
>> > #lang racket
>> > (define c%
>> >   (class* object% ()
>> >     (field [x 1])
>> >     (define y 2)
>> >     (super-new)))
>> > (define o (new c%))
>> > (field-names o)
>> > (get-field x o)
>> > (get-field y o)
>> > --
>> > I agree that 'define' is like making a field, but fields are something
>> > special too.
>> > Jay
>> > On Thu, Dec 16, 2010 at 6:51 AM, Robby Findler
>> > <robby at eecs.northwestern.edu>
>> > wrote:
>> >>
>> >> On Thu, Dec 16, 2010 at 12:22 AM, Mark Engelberg
>> >> <mark.engelberg at gmail.com> wrote:
>> >> > OK, it works when the set! occurs after the super-new.  I didn't
>> >> > think
>> >> > set!
>> >> > would work at all in a class definition (as opposed to within a
>> >> > method);
>> >> > I
>> >> > was thinking of the whole system of defining classes as more of a
>> >> > declarative DSL that only allowed certain constructs.
>> >>
>> >> You've probably already figured this out, but the body of a class is a
>> >> series of definitions and expressions like at the top-level but
>> >> 'define' taking on the meaning of 'make a field', and a bunch of new
>> >> definitions appearing. The new stuff says what the methods are, but
>> >> everything else is just executed in sequence as if it were in the body
>> >> of the initializer (if this were in Java, say).
>> >>
>> >> hth,
>> >> Robby
>
>
>
> --
> Jay McCarthy <jay at cs.byu.edu>
> Assistant Professor / Brigham Young University
> http://faculty.cs.byu.edu/~jay
>
> "The glory of God is Intelligence" - D&C 93
>


Posted on the dev mailing list.