Skip to content
Prev 388982 / 398506 Next

What if there's nothing to dispatch on?

On 31/08/2021 11:59 p.m., Rolf Turner wrote:
That design is probably not a good idea:  what if one of the variables 
in data matches the name of some other object in the global environment? 
  Then it would dispatch on that other object, and things won't go well.

But here's a way to shoot yourself in the foot:

function(x) {
   x1 <- try(x, silent = TRUE)
   if (inherits(x1, "try-error"))
     foo.default(x)
   else
     UseMethod("foo", x)
}

Happy shooting!

Duncan