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>}

Automatic storage-management note: Dylan provides automatic storage management (also called garbage collection). Thus, you do not need to deallocate memory explicitly. When an object becomes inaccessible, Dylan's automatic storage management will recycle the storage used by that object. In this section, there are two examples of objects that become inaccessible:

  • We redefined the <time-of-day> class. The storage used by the old class definition can be recycled.

  • We stored a new instance in *my-time-of-day*. The storage used by the instance previously stored in that variable can be recycled.

Although redefinition is not part of the Dylan language, most Dylan development environments support redefinition.

Comparison with Java: Java recognizes that manual memory management can be the source of program errors and often can be exploited to breach security measures. Like Dylan, Java has an automatic garbage collector that correctly and efficiently recovers unused objects in a program — freeing the programmer of that mundane but difficult chore.