[Rd] Suggestion for exception handling: More informative error message for "no applicable method..." (S3)
Henrik Bengtsson
hb at stat.berkeley.edu
Sat Oct 31 06:48:42 CET 2009
Thanks for this silent update, whoever did it:
> foo <- function(...) UseMethod("foo")
> foo.ClassA <- function(object, ...) { cat("foo() for ClassA called.\n") }
> foo(structure(1, class="ClassA"))
foo() for ClassA called.
> foo(NULL)
Error in UseMethod("foo") :
no applicable method for 'foo' applied to an object of class "NULL"
> foo(Sys.time())
Error in UseMethod("foo") :
no applicable method for 'foo' applied to an object of class
"c('POSIXt', 'POSIXct')"
This is great.
> sessionInfo()
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:
> 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(structure(1, class="ClassA"))
> foo() for ClassA called.
>
> Now:
>
>> foo(NULL)
> Error in UseMethod("foo") : no applicable method for "foo"
>
>> foo(Sys.time())
> Error in UseMethod("foo") : no applicable method for "foo"
>
> Suggestion:
>
>> foo(NULL)
> Error in UseMethod("foo") : no applicable foo() method: list
>
>> foo(Sys.time())
> 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.
>
More information about the R-devel
mailing list