12.3.3 Creation of methods
You can create a method in the following ways:
You can create one explicitly by
define method. This definition also adds the method to a generic function, creating the generic function if the latter does not already exist.You can create one explicitly by a
methodstatement. This statement does not add the method to a generic function.You can create one explicitly by a
local methoddeclaration. This declaration creates one or more methods, and assigns each to a local variable such that the binding is visible to all other methods defined in the samelocaldeclaration. This declaration does not add the method to a generic function.You can create one implicitly by defining a slot (other than a virtual slot) in
define class. Dylan defines a getter method for the slot, and adds it to a generic function, creating the generic function if that function does not already exist.You can create one implicitly by defining a slot (other than a virtual or a constant slot) in
define class. Dylan defines a setter method for the slot, and adds it to a generic function, creating the generic function if that function does not already exist.
Creating a method by using method is useful when the method does not need to be part of a generic function. For instance, various Dylan functions take as arguments other functions that act as predicates, or test functions. One of these is choose, which selects members of a sequence that satisfy a test function, and returns those members as a new sequence. We might pick all the strings out of a mixed sequence as follows:
define method choose-strings
(sequence :: <sequence>) => (new-seq :: <sequence>)
// choose takes two arguments: a function and a sequence
choose(method (object) instance?(object, <string>) end method, sequence);
end method choose-strings;
Creating a method by using local method is useful for a method that does not need to be part of a generic function, but does need to be given a name so that it can call itself recursively, or so that other code in the enclosing body can refer to it. For an example, see Section 11.3.6, page 147.




