[plt-scheme] protected members in class.ss?

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sun Jul 30 22:54:52 EDT 2006


Robby Findler wrote:
> At Sun, 30 Jul 2006 19:55:29 -0400, Jon Rafkind wrote:
>   
>> Hi,
>>     I was wondering if there was some way to make a method "protected"
>> like in C++ and Java wherein a method can only be called by sub-classes.
>> I read the documentation for class.ss forwards and backwards but didnt
>> find it.
>>     
>
> There isn't protected ala Java per se, but you can achieve the effect
> of hiding certain methods (only allowing them in certain scopes) via
> define-local-member-name. It's a different mechanism, but should help
> you do what you want (ie, encapsulate).
>
>   
Ok, I suppose that suffices for my purposes but it seems like it might
be convenient to define classes in separate parts of the program that
dont have access to the scope that define-local-member-name was used.
For instance

;; im assuming this is how you would use it to achieve protected members.
;; parens are totally made up and probably mismatched
(define-values (base sub1)
  (let ()
    (define-local-member-name apple)
    (let* ((base (class* object% ()
                         (define/public apple)))
            (sub1 (class* base ()
                         (define/public apple)))
      (values base sub1)))

;; some later point in the program

(define (make-new-thing)
  (class* base ()
     (define/public apple)))

Maybe I'm just thinking to much in the Java/C++ OOP way?


Posted on the users mailing list.