The code used by an object to respond to a message is called a method. Objects are organized in a hierarchy in which objects inherit from other objects. If an object does not have a method of its own for responding to a message it will use a method inherited from one of its ancestors. The "2D send function will move up the predecence list of an object's ancestors until a method for a message is found.
Most of the objects encountered so far inherit directly from prototype objects. Scatterplots inherit from "2D scatterplot-proto, histograms from "2D histogram-proto, regression models from "2D regression-model-proto. Prototypes are just like any other object. They are essentially typical versions of a certain kind of object that define default behavior. Almost all methods are owned by prototypes. But any object can own a method, and in the process of debugging a new method it is often better to attach the method to a separate object constructed for that purpose instead of the prototype.
To add a method to an object you can use the "2D defmeth
macro. As an example, in Section we calculated Cook's
distances for a regression model. If you find that
you are doing this very frequently then you might want to define a
"2D :cooks-distances method.
The general form of a method definition is:
(defmeth object :new-method arg-list body)"2D object is the object that is to own the new method. In the case of regression models this can be either a specific regression model or the prototype that all regression models inherit from, "2D regression-model-proto. The argument "2D :new-method is the message selector for your method; in our case this would be "2D :cooks-distances. The argument "2D arg-list is the list of argument names for your method, and "2D body is one or more expressions making up the body of the method. When the method is used each of these expressions will be evaluated, in the order in which they appear.
Here is an expression that will install the "2D :cooks-distances method:
(defmeth regression-model-proto :cooks-distances () " Message args: () Returns Cooks distances for the model." (let* ((leverages (send self :leverages)) (studres (/ (send self :residuals) (* (send self :sigma-hat) (sqrt (- 1 leverages))))) (num-coefs (send self :num-coefs))) (* (^ studres 2) (/ leverages (- 1 leverages) num-coefs)))))The variable "2D self refers to the object that is receiving the message.