This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: closures for GOOPS privates
- To: Mikael Djurfeldt <djurfeldt at nada dot kth dot se>
- Subject: Re: closures for GOOPS privates
- From: Mikael Djurfeldt <mdj at thalamus dot nada dot kth dot se>
- Date: 26 Sep 1999 22:31:37 +0200
- Cc: clark dot mcgrew at sunysb dot edu, guile at sourceware dot cygnus dot com
- Cc: djurfeldt at nada dot kth dot se
- References: <m11VGgC-000UuqC@boxer.physics.sunysb.edu> <xy7u2oh3asz.fsf@thalamus.nada.kth.se>
- Reply-to: Mikael Djurfeldt <djurfeldt at nada dot kth dot se>
Mikael Djurfeldt <mdj@thalamus.nada.kth.se> writes:
> Clark McGrew <mcgrew@ale.physics.sunysb.edu> writes:
>
> > In some scheme object systems it's possible to use closures to
> > associate private data with an object. Is it possible to do this with
> > GOOPS? I haven't had much time to play with GOOPS it doesn't appear
> > that it can be done.
>
> What is private data?
>
> If "private" means data that only can be accessed by a certain group
> of methods, then the most straightforward method to give them private
> data is to put the data together with this dedicated group in one
> module.
Hmm... This was quite a bit unclear. I didn't really mean "put the
data" but "puta accessors for the data" into a module.
In the (strange) example below `d' is a "private" slot used for
memoization of the length of the vector (abs v).
(define-module (math 2D-vector)
:use-module (oop goops))
(export <2D-vector> x y) ; x and y are "public" slots.
(define-class <2D-vector> ()
(x #:accessor x' #:init-value 0)
(y #:accessor y' #:init-value 0)
(d #:accessor d #:init-value #f))
(define-method x ((v <2D-vector>))
(x' v))
(define-method (setter v) ((v <2D-vector>) (value <real>))
(set! (x' v) value)
(set! (d v) #f))
;; ...similar for y...
;; Uses memoization
(define-method abs ((v <2D-vector>))
(if (d v)
d
(let ((res (sqrt (* (x' v) (x' v)) (* (y' v) (y' v)))))
(set! (d v) res)
res)))
Note also the following possibility:
(define-accessor x)
(define-accessor y)
(define <2D-vector>
(let ((d (make-accessor 'd)))
;; ...
;; Here you can do all sorts of things which will belong to a
;; "private" environment. Note that the accessor `d' only exists
;; within this environment.
;; ...
(class ()
(x #:accessor x #:init-value 0)
(y #:accessor y #:init-value 0)
(d #:accessor d #:init-value #f))))