[racket-dev] [racket] Question about fields in Racket OO
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.
Now that you point it out though, I see there is an example in the guide
that does set! on an inherited field.
Thanks,
Mark
On Wed, Dec 15, 2010 at 9:53 PM, Robby Findler
<robby at eecs.northwestern.edu>wrote:
> set!?
>
> Try it in both positions (the commented out one and the other one):
> the thing to keep in mind is that the declaration in c% is also kind
> of like a set! that happens when the object is initialized.
>
> Robby
>
> #lang racket
> (define c%
> (class object%
> (field [f 1])
> (define/public (get-f) f)
> (super-new)))
>
> (define d%
> (class c%
> (inherit-field f)
> (set! f 2)
> (super-new)
> ; (set! f 2)
> ))
>
> (send (new d%) get-f)
>
> On Wed, Dec 15, 2010 at 11:46 PM, Mark Engelberg
> <mark.engelberg at gmail.com> wrote:
> > Thanks. That seems to address the shared private issue.
> >
> > So is there a way to give a new value to an inherited field?
> >
> > --Mark
> >
> > On Wed, Dec 15, 2010 at 9:13 PM, Robby Findler <
> robby at eecs.northwestern.edu>
> > wrote:
> >>
> >> See define-local-member-name.
> >>
> >> Robby
> >>
> >> On Wed, Dec 15, 2010 at 10:38 PM, Mark Engelberg
> >> <mark.engelberg at gmail.com> wrote:
> >> > I'm playing around with the object-oriented subset of Racket, and have
> a
> >> > couple of questions.
> >> >
> >> > Ideally, I'd like to equip a class with a field that is visible only
> to
> >> > it
> >> > and its subclasses. As far as I can tell, though, this isn't
> possible.
> >> > It
> >> > seems that I have to make a choice between a completely private field
> >> > visible only to the class (by just using "define") or making a
> >> > completely
> >> > public field (by using "field"). Correct?
> >> >
> >> > Now, let's say I make the field public.
> >> >
> >> > In the subclass, how do I change the default value of field?
> >> >
> >> > For example, in the superclass, I might have
> >> > (field [a 300])
> >> >
> >> > but in the subclass, I want to do something like
> >> > (inherit-field [a 200])
> >> >
> >> > However, as far as I can tell, the syntax doesn't support anything
> other
> >> > than
> >> > (inherit-field a)
> >> >
> >> > Thanks,
> >> >
> >> > Mark
> >> >
> >> > _________________________________________________
> >> > For list-related administrative tasks:
> >> > http://lists.racket-lang.org/listinfo/users
> >> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/dev/archive/attachments/20101215/bbbd3af1/attachment.html>