Next: , Previous: , Up: Yasos   [Contents][Index]

3.14.3 Setters

Setters implement generalized locations for objects associated with some sort of mutable state. A getter operation retrieves a value from a generalized location and the corresponding setter operation stores a value into the location. Only the getter is named – the setter is specified by a procedure call as below. (Dylan uses special syntax.) Typically, but not necessarily, getters are access operations to extract values from Yasos objects (see Yasos). Several setters are predefined, corresponding to getters car, cdr, string-ref and vector-ref e.g., (setter car) is equivalent to set-car!.

This implementation of setters is similar to that in Dylan(TM) (Dylan: An object-oriented dynamic language, Apple Computer Eastern Research and Technology). Common LISP provides similar facilities through setf.

Function: setter getter

Returns the setter for the procedure getter. E.g., since string-ref is the getter corresponding to a setter which is actually string-set!:

(define foo "foo")
((setter string-ref) foo 0 #\F) ; set element 0 of foo
foo ⇒ "Foo"
Syntax: set place new-value

If place is a variable name, set is equivalent to set!. Otherwise, place must have the form of a procedure call, where the procedure name refers to a getter and the call indicates an accessible generalized location, i.e., the call would return a value. The return value of set is usually unspecified unless used with a setter whose definition guarantees to return a useful value.

(set (string-ref foo 2) #\O)  ; generalized location with getter
foo ⇒ "FoO"
(set foo "foo")               ; like set!
foo ⇒ "foo"
Procedure: add-setter getter setter

Add procedures getter and setter to the (inaccessible) list of valid setter/getter pairs. setter implements the store operation corresponding to the getter access operation for the relevant state. The return value is unspecified.

Procedure: remove-setter-for getter

Removes the setter corresponding to the specified getter from the list of valid setters. The return value is unspecified.

Syntax: define-access-operation getter-name

Shorthand for a Yasos define-operation defining an operation getter-name that objects may support to return the value of some mutable state. The default operation is to signal an error. The return value is unspecified.

Next: , Previous: , Up: Yasos   [Contents][Index]