[racket] Calling Private Methods on non-this Objects?

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Thu May 30 19:17:50 EDT 2013

PS: racket's class system has classes-as-values and some of the things that
are allowed in those language depend on first-order classes (like that
feature) and that extra power that you get means that some things come in
different ways that you're expecting. FWIW.

Robby

On Thursday, May 30, 2013, Sean Kanaley wrote:

> Yes but that only works for "this".  It's basically (send this method args
> ...) == (method args ...).  I wish to call the method on a *different*object of the same class.  C++/Java/C# etc. all allow access to private
> data in "sibling" objects.
>
>
> On Thu, May 30, 2013 at 7:09 PM, Robby Findler <
> robby at eecs.northwestern.edu <javascript:_e({}, 'cvml',
> 'robby at eecs.northwestern.edu');>> wrote:
>
>> When you are inside a class, you don't use send. You just call the method
>> with a regular function-application looking syntax.
>>
>> #lang racket
>> (define c%
>>   (class object%
>>     (define/public (m x)
>>       (printf "n on ~s is ~s\n" x (n x)))
>>     (define/private (n x)
>>       (* x x))
>>     (super-new)))
>>
>> (send (new c%) m 11)
>>
>>
>> Robby
>>
>>
>> On Thursday, May 30, 2013, Sean Kanaley wrote:
>>
>>> In C++ for example, the following is valid:
>>>
>>> class A {
>>> private:
>>>     int test(A a) { return n + a.n; }
>>>     int n;
>>> };
>>>
>>> The key point is the "a.n" is valid.
>>>
>>> I'm trying to create a 3d game in Racket, and in order to avoid
>>> recomputing world transforms all the time, child objects (say a rotatable
>>> gun on a parent tank) take a parameter to their parent which is used to add
>>> the child ("this") to the parent, in order that the parent update a delayed
>>> world transform computation in case of multiple calls to set-trans!,
>>> roughly:
>>>
>>> (define obj%
>>>   (class object% (super-new) (init ... [parent #f])
>>>     (define p parent)
>>>     (define cs '())
>>>     (when p (send p add-child! this))
>>>     (define/public (set-trans! new-t)
>>>       ... <includes delayed world-trans calc>
>>>       (for ([c cs])
>>>         (send c set-trans! (send c local-trans))))
>>>     ...
>>>     (define/public/private/etc. (add-child! c) (set! cs (cons c cs)))))
>>>
>>> It obviously works with "define/public", but I'm hoping there is a way
>>> to not expose the method everywhere.  It's in essence private, but Racket
>>> seems to not allow access even from within the class (send complains "no
>>> such method").
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130530/98681bbc/attachment-0001.html>

Posted on the users mailing list.