7.1 Functions and methods
To add a method to the generic function browser, define the method by typing directly into the Listener. Then, choose Works > Update and the browser will find the new method.
As an example, consider the methods defined in the examples file, animals.lsp
, used earlier. Make sure that you have evaluated the code in this file before proceeding.
1. Browse the feed
generic function defined in the examples file.
To do this, display a generic function browser and type feed
into the 'Function' text box.
Note that there are methods defined on many classes of animal, but remember that each of these classes has itself at least one superclass. For instance, dog
, sheep
and tiger
are all subclasses of the class mammal
, which is itself a subclass of animal
.
We now define two new methods -- one on the mammal
class and one on the animal
class.
2. In the listener, type the following:
CL-USER 45 > (defmethod feed ((arg mammal)) 'yum-yum) #<STANDARD-METHOD FEED NIL (MAMMAL) 18DCEB0> CL-USER 46 >This defines a new
feed
method on the mammal
class.3. Select Works > Update.
Note that the method you just defined is now visible (as (MAMMAL)
) in the 'Methods' and 'Signatures' list panes.
We could now make an instance of a mammal with, for example,
CL-USER 46 > (setq rover (make-instance 'mammal)) #<MAMMAL 1831A8> CL-USER 47 >and we could feed this mammal with
CL-USER 47 > (feed rover) YUM-YUM CL-USER 48 >4. Return to the listener and type:
CL-USER 48 > (defmethod feed ((arg animal)) 'slurp) #<STANDARD-METHOD FEED NIL (ANIMAL) 18dceb0>This defines a method on the
animal
class.5. Select Works > Update again.
To remove any method defined, simply select it in the 'Methods' list pane and choose Method > Remove.
Generated with Harlequin WebMaker