[plt-scheme] protected members in class.ss?
If you use define-local-member-name at the top-level of a module, you
can export the name wherever you wish.
(module m mzscheme
(require (lib "class.ss"))
(define-local-member-name method-for-my-friends)
(define c%
(class object%
(define/public (method-for-my-friends x)
(printf "x ~s\n" x))
(super-new)))
(provide c% method-for-my-friends))
(module n mzscheme
(require m
(lib "class.ss"))
(send (new c%) method-for-my-friends 1))
Robby
At Sun, 30 Jul 2006 22:54:52 -0400, Jon Rafkind wrote:
>
>
> 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?