Node:Accessing Slots, Next:Creating Generic Functions, Previous:Creating Instances, Up:Reference Manual
The definition of a slot contains at the very least a slot name, and may also contain various slot options, including getter, setter and/or accessor functions for the slot.
It is always possible to access slots by name, using the various
"slot-ref" and "slot-set!" procedures described in the following
subsections. For example,
(define-class <my-class> () ;; Define a class with slots
(count #:init-value 0) ;; named "count" and "cache".
(cache #:init-value '())
...)
(define inst (make <my-class>)) ;; Make an instance of this class.
(slot-set! inst 'count 5) ;; Set the value of the "count"
;; slot to 5.
(slot-set! inst 'cache ;; Modify the value of the
(cons (cons "^it" "It") ;; "cache" slot.
(slot-ref inst 'cache)))
If a slot definition includes a getter, setter or accessor function,
these can be used instead of slot-ref and slot-set! to
access the slot.
(define-class <adv-class> () ;; Define a new class whose slots
(count #:setter set-count) ;; use a getter, a setter and
(cache #:accessor cache) ;; an accessor.
(csize #:getter cache-size)
...)
(define inst (make <adv-class>)) ;; Make an instance of this class.
(set-count inst 5) ;; Set the value of the "count"
;; slot to 5.
(set! (cache inst) ;; Modify the value of the
(cons (cons "^it" "It") ;; "cache" slot.
(cache inst)))
(let ((size (cache-size inst))) ;; Get the value of the "csize"
...) ;; slot.
Whichever of these methods is used to access slots, GOOPS always calls
the low-level getter and setter closures for the slot to get
and set its value. These closures make sure that the slot behaves
according to the #:allocation type that was specified in the slot
definition (see allocation). (For more about these
closures, see compute-get-n-set.)