I want to implement an inset method for my class myClass
for the internal generic [<-
(~ help(Extract)
).
This method should run a bunch of tests, before passing on the actual insetting off to [<-
via NextMethod()
.
I understand that:
- any method has to include at least the arguments of the generic (mine does, I think)
- the
NextMethod()
call does not usually need any arguments (though supplying them manually doesn't seem to help either).
Here's my reprex:
x <- c(1,2)
class(x) <- c("myClass", "numeric")
`[<-.myClass` <- function(x, i, j, value, foo = TRUE, ...) {
if (foo) {
stop("'foo' must be false!")
}
NextMethod()
}
x[1] <- 3 # this errors out with *expected* error message, so dispatch works
x[1, foo = FALSE] <- 3 # this fails with "incorrect number of subscripts
What seems to be happening is that NextMethod()
also passes on foo
to the internal generic [<-
, which mistakes foo
for another index, and, consequently errors out (because, in this case, x
has no second dimension to index on).
I also tried supplying the arguments explicitly no NextMethod()
, but this also fails (see reprex below the break).
How can I avoid choking up NextMethod()
with additional arguments to my method?
(Bonus: Does anyone know good resources for building methods for internal generics? @Hadleys adv-r is a bit short on the matter).
Reprex with explicit arguments:
x <- c(1,2)
class(x) <- c("myClass", "numeric")
`[<-.myClass` <- function(x, i = NULL, j = NULL, value, foo = TRUE, ...) {
if (foo) {
stop("'foo' must be false!")
}
NextMethod(generic = "`[<-`", object = x, i = i, j = j, value = value, ...)
}
x[1] <- 3 # this errors out with expected error message, so dispatch works
x[1, foo = FALSE] <- 3 # this fails with "incorrect number of subscripts