10.6.3 Initialize method for a virtual slot

We define the initialize method:

define method initialize (angle :: <directed-angle>, #key direction: dir)	//1
  next-method();	//2
  angle.direction := dir;	//3
end method initialize;	//4

For keyword parameters, the name of the keyword that you supply to make is normally the same name as the parameter that is initialized within the body. In this case, we want to avoid confusion between the getter direction and the keyword parameter direction:, so we use dir as the name of the keyword parameter for the initialize method. When you call make, you use the direction: keyword. However, within this method, the parameter is named dir.

Line 3 calls the setter for the direction slot. We defined the methods for direction-setter in Section 10.6.2. If the argument is a latitude, then method 3 is invoked to check the value. If the argument is a longitude, then method 4 is invoked to check the value.

We can create a new instance of <absolute-position>.

? define variable *my-absolute-position* = 
    make(<absolute-position>, 
         latitude:  
           make(<latitude>, 
                total-seconds: encode-total-seconds(42, 19, 34),
                direction: #"north"),
         longitude: 
           make(<longitude>,
                total-seconds: encode-total-seconds(70, 56, 26),
                direction: #"west")); 

The preceding example works, because the values for direction are appropriate for latitude and longitude. The following example shows what happens when the direction is not valid when an instance is created:

? make(<latitude>, direction: #"nooth");
ERROR: nooth is not north or south 

The following example shows what happens when the direction is not valid when the direction setter is used:

? begin
    let my-longitude = make(<longitude>, direction: #"east");
    my-longitude.direction := #"north";
  end;
ERROR: north is not east or west