4.5.2 Init keywords: Keywords that initialize slots
The total-seconds: keyword is an init keyword — a keyword that we can give to make to provide an initial value for a slot. To make it possible to give an init keyword to make, we need to use the init-keyword: slot option when we define the class. A slot option lets us specify a characteristic of a slot. Slot options appear after the optional type specifier of a slot.
Here, we redefine the <time-of-day> class to use the init-keyword: slot option:
// A specific time of day from 00:00 (midnight) to below 24:00 (tomorrow) define class <time-of-day> (<object>) // 1 slot total-seconds :: <integer>, init-keyword: total-seconds:; // 2 end class <time-of-day>; // 3
The preceding definition redefines the class <time-of-day>. That is, this new definition of <time-of-day> replaces the old definition of <time-of-day>.
In line 2, the init-keyword: slot option defines total-seconds: as a keyword parameter that we can give to make when we make an instance of this class. Now that we have defined total-seconds: as an init keyword, we can provide the keyword argument as follows:
? *my-time-of-day* := make(<time-of-day>, total-seconds: 120);
{instance of <time-of-day>}
The preceding expression creates a new instance of <time-of-day>, and stores that instance in the variable *my-time-of-day*. The value of the total-seconds slot of this instance is initialized to 120. The assignment operator returns the new value stored; in the preceding call, the new value is the newly created instance of <time-of-day>, which the listener displays as {instance of <time-of-day>}.
We can use the getter to verify that the slot has an initial value:
? *my-time-of-day*.total-seconds; 120
If you call make and provide a keyword that has not been declared as a valid keyword for the class, you get an error; for example,
? make(<time-of-day>, seconds: 120);
ERROR: seconds: is not a valid keyword argument to make for {class <time-of-day>}




