When a generic (S4) has an argument with a default followed by ..., missing()
doesn't seem to work if the method omits the ...
--------------------Sample-------------------------------
foo <- function(x, y=0, ...){
"you are very generic"
}
# no ... in function arguments
setMethod("foo", signature="character", function(x, y=0){
if (missing(y))
return("must give y for character")
y
})
setMethod("foo", signature="numeric", function(x, y=0, ...){
if (missing(y))
return("must give y for numeric")
y
})
print(foo("a")) #[1] 0
print(foo(0)) #[1] "must give y for numeric"
------------------------------------------------------------------
It's the result for foo("a") I'm puzzled by, since missing(y) does not evaluate
to TRUE.
Background
==========
The methods documentation has 2 points on which the above definitions may fail.
1. The generic has regular arguments and ... arguments. But dotsMethods docs
say
> either the signature of the generic function is "..." only, or it
> does not contain "..."
Since the arguments in ... are not part of the signatures I think I'm OK, but
another reading is that
one just shouldn't mix ... and other arguments.
2. setMethod docs say
> The definition must be a function with the same formal arguments as the
generic; however, setMethod() will handle methods that add arguments, if ... is
a formal argument to the generic.
Since the initial definition has arguments x, y, ... and the first method
definition has only x, y, the arguments don't match. So maybe that's the
problem.
I don't know if the fact that y has a default value matters.
The real code has a function f that ordinarily requires an additional piece of
information, y, to compute a result. But for one class, the result doesn't
depend on y and so that argument may be omitted.
Context
======
R 4.3.3 on MS-Windows under RStudio 2023.12.1 build 402.
Thanks for any insights.
Ross
______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.