Skip to content

Suggestion for exception handling: More informative error message for "no applicable method..." (S3)

2 messages · Henrik Bengtsson

#
I'd like to suggest that whenever there is no S3 method implementation
available for a particular class, that the error message would also
report the class structure of the object dispatched on.

Example:

foo <- function(...) UseMethod("foo")
foo.ClassA <- function(object, ...) { cat("foo() for ClassA called.\n") }
foo() for ClassA called.

Now:
Error in UseMethod("foo") : no applicable method for "foo"
Error in UseMethod("foo") : no applicable method for "foo"

Suggestion:
Error in UseMethod("foo") : no applicable foo() method: list
Error in UseMethod("foo") : no applicable foo() method: POSIXt, POSIXct

This would really help troubleshooting, especially when running in
batch mode where you don't have direct access to the object without
modifying the script and/or functions.

/Henrik

PS. I know that one as a workaround can create a "default" method that
reports this, but I believe it is much better that the builtin
exception handling should report this/carry this information.
10 days later
#
Thanks for this silent update, whoever did it:
foo() for ClassA called.
Error in UseMethod("foo") :
  no applicable method for 'foo' applied to an object of class "NULL"
Error in UseMethod("foo") :
  no applicable method for 'foo' applied to an object of class
"c('POSIXt', 'POSIXct')"

This is great.
R version 2.10.0 Patched (2009-10-26 r50212)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

/Henrik
On Tue, Oct 20, 2009 at 12:44 PM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote: