Skip to content
Prev 22095 / 63424 Next

generics for primitive functions

Probably a bug, but not at all the one you imply.  You have found a way 
to subvert a guarantee in R that methods for primitives can never be 
redefined for basic data types.

As you no doubt found, but didn't show us, so long as you say sqrt(4) 
the result is correct.  (Your "Works" should really read "Gotcha!").

Since a large amount of effort and not a little programming pain has 
been spent retaining the purity of primitive calls to satisfy the 
efficiency lobby, it's not clear what should happen when one insists on 
calling the generic function directly.  Either an error or just a call 
to the primitive would be the two likely candidates.  Not what happens 
in your first example though.

If you really want a "numeric" class that does something different for 
sqrt(), define an S4 class that contains "numeric"
 
 > setClass("WeirdNumber", contains="numeric")
[1] "WeirdNumber"
 > setMethod("Math", "WeirdNumber", function(x) "Weird")
[1] "Math"
 > xx = new("WeirdNumber", 1:10)
 > sqrt(xx)
[1] "Weird"
Parlamis Franklin wrote: