5.5.1 Step 1: Find the applicable methods

Start with the set of methods defined for the generic function that was called. A method is specialized on a required parameter that has a type constraints. The type constraint of the required parameter is called the parameter specializer of the parameter. A method is applicable if the argument to the generic function is an instance of the parameter specializer of the method.

For example, consider the decode-total-seconds generic function. Table 5.1 shows which method is applicable for certain arguments.

Table 5.1 Applicable methods for arguments to decode-total-seconds.

Argument

Argument's type

Applicable methods

*my-time-of-day*

<time-of-day>

method on <time>

*my-time-offset*

<time-offset>

method on <time>

1000

<integer>

method on <integer>

"hello, world"

<string>

none

The first row of the table shows that, when the argument is a direct instance of <time-of-day>, the method on <time> is applicable, because the argument is an instance of <time> (the method's parameter specializer). The final row of the table shows that, when the argument is "hello, world", none of the defined methods are applicable, because "hello, world" is not an instance of <time> or <integer>.

For decode-total-seconds, there is either no or one applicable method for any argument. If there is one applicable method, it is called. If there is no applicable method, the "No applicable method" error is signaled. There is no need to continue to step 2.

In other cases, there can be several applicable methods. Consider the generic function say-greeting, shown in Figure 5.4. Table 5.2 shows that, for certain arguments, one method is applicable, but that, for an integer argument, two methods are applicable.

When the argument is 7, a direct instance of <integer>, the method on <object> is applicable, because 7 is an instance of <object> (the method's parameter specializer); the method on <integer> also is applicable, because 7 is an instance of <integer> (the method's parameter specializer).

Figure 5.4 The say-greeting generic function and its methods.
Generic function say-greeting
define method say-greeting (greeting :: <object>)
  format-out("%s\n", greeting);
end;
define method say-greeting (greeting :: <integer>)
  format-out("Your lucky number is %s.\n", greeting);
end;
Table 5.2 Applicable methods for different arguments to say-greeting.

Argument

Applicable method(s)

7

method on <object> method on <integer>

$pi

method on <object>

"hello, world"

method on <object>