One thing to realize is that "methods" in R are very different to those in
other common "object-oriented" languages such as C++, Java, or Python. One
difference is that methods are not intimately associated with a class
definition -- the method dispatch system pulls disparate pieces of
information together. Another difference is that the choice of which
method to invoke can depend on all arguments, not just the first.
Yet another thing to realize is that R has two class systems (commonly
known as something like "S3" and "S4" methods and classes). Each has its
own way of defining classes, generic functions and methods. The S4 class
system offers more control over which method is called. Consult the "Green
Book" for details of the S4 system. (See the FAQ for full details of the
"Green Book").
Here's an example of something like what you are asking for, using the S4
class system. Note that this example doesn't involve the definition of any
classes, just a generic and methods for some standard classes. Also note
that a generic and its methods should have the same argument names.
setGeneric("foo", function(x,y) standardGeneric("foo"))
setMethod("foo", c("ANY", "missing"),
+ function(x,y) "x:any y:missing")
[1] "foo"
setMethod("foo", c("numeric", "numeric"),
+ function(x,y) "x:numeric y:numeric")
[1] "foo"
setMethod("foo", c("missing", "numeric"),
+ function(x,y) "x:missing y:numeric")
[1] "foo"
[1] "x:numeric y:numeric"
[1] "x:missing y:numeric"
Error in foo(y = "bar") : no direct or inherited method for function 'foo'
for this call
(You can execute the above commands under Windows by copying the entire
transcript to the clipboard, and then using the "Paste commands only" menu
item under the "Edit" menu in the "R console" window.)
hope this helps to at least get you started,
Tony Plate