I have a class myclass
in an R package for which I would like to define a method as.raw
, so of the same name as the primitive function as.raw()
. If constructor, generic and method are defined as follows...
new_obj <- function(n) structure(n, class = "myclass") # constructor
as.raw <- function(obj) UseMethod("as.raw") # generic
as.raw.myclass <- function(obj) obj + 1 # method (dummy example here)
... then R CMD check
leads to:
Warning: declared S3 method 'as.raw.myclass' not found
See section ‘Generic functions and methods’ in the ‘Writing R
Extensions’ manual.
If the generic is as_raw
instead of as.raw
, then there's no problem, so I assume this comes from the fact that the primitive function as.raw
already exists. Is it possible to 'overload' as.raw
by defining it as a generic (or would one necessarily need to use a different name?)?
Update: NAMESPACE
contains
export("as.raw") # export the generic
S3method("as.raw", "myclass") # export the method
This seems somewhat related, but dimnames
there is a generic and so there is a solution (just don't define your own generic), whereas above it is unclear (to me) what the solution is.