10.3 Initialize methods
Every time you call make to create an instance of a class, make calls the initialize generic function. The purpose of the initialize generic function is to initialize the instance before it is returned by make. You can customize the initialization by defining a method on initialize. Methods for initialize receive the instance as the first argument, and receive all keyword arguments given in the call to make.
We define an initialize method:
define method initialize (time :: <time-of-day> #key) // 1
next-method(); // 2
if (time.total-seconds < 0) // 3
error("%d is invalid. total-seconds cannot be negative", // 4
time.total-seconds); // 5
end if; // 6
end method initialize; // 7
On line 2, we call next-method. All methods for initialize should call next-method as their first action, to allow any less specific initializations (that is, initialize methods defined on superclasses) to execute first. If you call next-method as the first action, then, in the rest of the method, you can operate on an instance that has been properly initialized by any initialize methods of superclasses. If you forget to include the call to next-method, your initialize method will be operating on an improperly initialized instance.
Lines 3 through 6 contain the real action of this method. We check that the value is valid. If it is invalid, we signal an error.
The following example shows what happens when total-seconds is not valid when we are creating an instance:
? make(<time-of-day>, total-seconds: -15); ERROR: -15 is invalid. total-seconds cannot be negative.




