Methods Invoked from S-PLUS Functions

DESCRIPTION:
These are utility functions for object-oriented programming.

USAGE:
UseMethod(generic, object, ...)
NextMethod(generic, object, ...)

OPTIONAL ARGUMENTS:
generic:
character string naming the generic function. While this can usually be inferred by the implementation in typical applications, it is strongly encouraged to supply this argument explicitly. For one thing, the semantics of the function containing the call is then independent of any object names. Specifically, use of the function in contexts like apply will not work otherwise. It is also good style to include this argument if the name of the generic function includes a period (.).
object:
the object to use in determining classes. By default, the first argument to the function is used; you need to specify the relevant argument if it is not the first one.

VALUE:
the value of the invoked method, or NULL if there is no method (not even a default method).

SIDE EFFECTS:
any side effects performed by the method that is used.

In addition, the DETAILS section discusses the manipulation of function frames that occurs.


DETAILS:
These functions implement class-dependent methods in S-PLUS. A generic function abc may call UseMethod in order to examine its first argument and dispatch a method appropriate to the argument's class. For example, if abc was given an argument whose class contains "def", then function abc.def will be dispatched if it exists. When UseMethod is executed, the frame of the function calling UseMethod is used to provide arguments to the dispatched method. The method matches its arguments from that frame. Since the method uses the original frame, commands after UseMethod in the generic function will not be executed.

The function NextMethod when called within the method, determines the next inherited method for this function and evaluates it, returning the value to the calling method. Calling NextMethod is the usual way to define a new method that elaborates on what would be done by an inherited method for the same function.

By default, the first argument to the generic function determines the relevant class, with the exception of infix operators. For operators, a method is defined if either of the operands defines a class for which a method exists; in the case that both operands define a method, the two classes must be the same.


REFERENCES:
Chambers, J. M. and Hastie, T. J. (1991). Statistical Models in S. Wadsworth & Brooks/Cole, Pacific Grove, CA. (Appendix A)

SEE ALSO:
Methods , class .

EXAMPLES:
# How function print uses methods
  print
function(x, ...)
UseMethod("print")

print.foo function(x, ...) { dd <- if(length(x) > 5) 4 else 12 # use invisible so the value won't be printed again invisible(NextMethod("print", digits = dd)) }