[plt-scheme] inheritance of private fields?
Yes, that's what I meant. Does the below working example help? (v
4.0) -- Matthias
#lang scheme
(require scheme/class)
(define-values (fish% pond% ) ;; two mutually recursive classes
(let () ; create a local definition scope
(define-member-name get-depth (generate-member-key))
(define fish%
(class object%
(super-new)
(define my-depth 10)
(define my-pond (new pond%))
(define/public (dive amt)
(set! my-depth
(min (+ my-depth amt )
(send my-pond get-depth)))
my-depth)))
(define pond%
(class object%
(super-new)
(define current-depth 200)
(define/public (get-depth) current-depth)))
(values fish% pond%)))
(define fish (new fish%))
(send fish dive 300)
(with-handlers ((exn:fail:object? (lambda (x) (printf "method get-
depth not found, as expected: ~e" x))))
(send fish get-depth))
On Feb 29, 2008, at 9:47 AM, tom sgouros wrote:
>
> Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>
>>
>> Take a look at the paper at
>>
>> http://www.ccs.neu.edu/scheme/pubs/#aplas06-fff
>>
>> It shows how to generate "private" names that you can share in a way
>> that's analogous to protected in Java but you can choose the
>> restriction policy via lexical scope.
>
> Thank you for the help, and I'm sorry to be so dense, but I've been
> looking at that paper for about half an hour, and I don't see which
> part
> is applicable. You're not talking about the generate-member-key
> business are you? Can you be more specific, please?
>
> My apologies,
>
> -tom
>
>>
>>
>> On Feb 29, 2008, at 7:45 AM, Tom Sgouros wrote:
>>
>>>
>>> Hello all:
>>>
>>> I want to define a general class with some private fields that
>>> can be
>>> used by subclass. Here's a dumb example of what I thought would be
>>> the
>>> right way to do this, but apparently isn't:
>>>
>>> (define size%
>>> (class* object% ()
>>> (public print-name print-age print-weight)
>>>
>>> (init-field (name 'Fred) (bday 070661)))
>>>
>>> (define age (calculate-age bday))
>>> (define weight 0)
>>>
>>> (define print-name (display name))
>>> (define print-age (display age))
>>> (define print-weight (display weight))
>>>
>>> (super-new)))
>>>
>>> Now I want to be able to define a sub-class of this, so that age and
>>> weight are available to definitions in that sub-class. I haven't
>>> been
>>> able to do that, so feel I must be missing something.
>>>
>>> In the example age is a calculated value that depends on other
>>> init-field values. I'd make it public by putting it into the init-
>>> field
>>> sequence, if there's a guarantee that the init-field entries are
>>> processed in order (that it's a let* and not a let). I can't
>>> find any
>>> such guarantee in the documentation. Failing that, what should I
>>> do to
>>> make it available?
>>>
>>> The weight private field should have no default value, but is
>>> intended
>>> to be given one by the sub-class. In my class-naif view of these
>>> things, I should really make size% an interface and force the sub-
>>> class
>>> to fill in the weight. But I can't seem to figure out how to do
>>> that,
>>> either.
>>>
>>> Can someone explain to me the proper way to deal with these issues?
>>> Any
>>> advice welcome.
>>>
>>> Many thanks,
>>>
>>> -tom
>>>
>>> --
>>> ------------------------
>>> tomfool at as220 dot org
>>> http://sgouros.com
>>> http://whatcheer.net
>>> _________________________________________________
>>> For list-related administrative tasks:
>>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>
>
> --
> ------------------------
> tomfool at as220 dot org
> http://sgouros.com
> http://whatcheer.net