[Rd] UseMethod() fails to (PR#1176)

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
16 Nov 2001 17:13:42 +0100


hb@maths.lth.se writes:

> match the argument 'x' if there is another argument with prefix 'x'
> 
> Full_Name: Henrik Bengtsson
> Version: 1.3.1
> OS: Sun Solaris 8
> Submission from: (NULL) (130.235.3.49)
> 
> 
> I ran into a strange problem with UseMethod(). Run the following code:
> 
> foo.Bar <- function(this, x=0, xidx=0) {
>   cat("In foo.Bar(): ", this, ", x=", x, ", xidx=", xidx, ".\n", sep="");
> }
> 
> foo.default <- function(x, ...) {
>   str("Error: No default function found!");
> }
> 
> foo <- function(x, ...) UseMethod("foo");
> 
> 
> obj <- "Hello world!";
> class(obj) <- "Bar";
> 
> foo(obj);
> foo(obj, xidx=1);
> foo(obj, x=1);             # <- PROBLEM HERE!
> foo(obj, x=1, xidx=1);     # <- PROBLEM HERE!
> foo(obj, xidx=1, x=1);     # <- PROBLEM HERE!
> foo.Bar(obj, x=1);
> foo.Bar(obj, x=1, xidx=1);
> 
> and you will get
> 
>   In foo.Bar(): Hello world!, x=0, xidx=0.
>   In foo.Bar(): Hello world!, x=0, xidx=1.
>    chr "Error: No default function found!"
>    chr "Error: No default function found!"
>    chr "Error: No default function found!"
>   In foo.Bar(): Hello world!, x=1, xidx=0.
>   In foo.Bar(): Hello world!, x=1, xidx=1.
> 
> There is something going on with matching of arguments; UseMethod() fails to
> match the argument 'x' if there is another argument with prefix 'x'.
> 
> I believe this is an unwanted feature of UseMethod.

Nope. In all the three cases you've marked as problems, you're telling
foo that the "x" argument is 1 (cf. plot(rnorm(10),x=1:10) ) and thus
UseMethod dispatches the default method.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._