10.6.2 Getter and setter methods for a virtual slot
Here is the getter method for the virtual slot direction:
// Method 1 define method direction (angle :: <directed-angle>) => (dir :: <symbol>) angle.internal-direction; end method direction;
Here are the setter methods for the virtual slot direction:
// Method 2
define method direction-setter
(dir :: <symbol>, angle :: <directed-angle>) => (new-dir :: <symbol>)
angle.internal-direction := dir;
end method direction-setter;
// Method 3
define method direction-setter
(dir :: <symbol>, latitude :: <latitude>) => (new-dir :: <symbol>)
if (dir == #"north" | dir == #"south")
next-method();
else
error("%= is not north or south", dir);
end if;
end method direction-setter;
// Method 4
define method direction-setter
(dir :: <symbol>, longitude :: <longitude>) => (new-dir :: <symbol>)
if (dir == #"east" | dir == #"west")
next-method();
else
error("%= is not east or west", dir);
end if;
end method direction-setter;
The preceding methods work as follows:
When you call
directionon an instance of<directed-angle>or any of its subclasses, method 1 is invoked. Method 1 calls the getterinternal-direction, and returns the value of theinternal-directionslot.When you call
direction-setteron a direct instance of<latitude>, method 3 is invoked. Method 3 checks that the direction is valid for latitude; if it finds that the direction is valid, it callsnext-method, which invokes method 2. Method 2 stores the direction in theinternal-directionslot.When you call
direction-setteron a direct instance of<longitude>, method 4 is called. Method 4 checks that the direction is valid for longitude; if it finds that the direction is valid, it callsnext-method, which invokes method 2. Method 2 stores the direction in theinternal-directionslot.When you call
direction-setteron a direct instance of<directed-angle>, method 2 is invoked. Method 2 stores the direction in theinternal-directionslot.
In these methods, we use dir, rather than direction, as the name of the parameter that represents direction. Recall that direction is the name of a getter. Although we technically could use direction as the parameter name in these methods (because we do not call the direction getter in the bodies), direction as a parameter name might be confusing to other people reading the code.
The error function signals an error. For more information about signaling and handling errors, see Chapter 20, Exceptions.
The direction-setter methods check the direction when the setter is called. In Section 10.6.3, we check the direction when an instance is made.




