Skip to content
Prev 7395 / 63421 Next

Questions about S4 style methods and generics

Saikat DebRoy wrote:
Hmmm, well "substituted" may not be the best term to use.

What happens here is pretty much determined by the API, not R-specific. 
Your question about defaults has come up before; should be in a
to-be-written FAQ.

Basically everything is standard S (R or S-Plus) up to the point that
standardGeneric is called.  With an ordinary generic function, arguments
are matched but not evaluated, because of lazy evaluation.

Now the dispatch code examines as many arguments as are involved in
existing signatures (2 in your example).  The class for each argument
inserted into the current signature for dispatch is:
  - "missing" if the corresponding argument is missing from the call;
  - the class of the evaluated actual argument otherwise.
Because of the first step, default expressions are not evaluated during
method selection; they obey the standard S rules of lazy evaluation, as
do arguments not needed for the signature.

(Not relevant to the question, but the dispatch is done first in C; if
there is no method matching the signature exactly, then S language code
is used to select a method by inheritance, and the selected method is
cached under the current signature.)

Your later example seems to be a bug of some sort, but I'll examine it
more closely.  Thanks.  (Dispatch on 3+ arguments probably hasn't had
much exercise!)

John