[Rd] Why does NextMethod() pick up duplicate arguments in '...' if given positionally at top level?
Michael Chirico
m|ch@e|ch|r|co4 @end|ng |rom gm@||@com
Tue Mar 25 07:12:26 CET 2025
Consider:
foo <- function(x, y, ...) {
UseMethod("foo")
}
foo.default <- function(x, y = 0, ...) {
cat(sprintf("%s: x=%s, y=%s\n", as.character(match.call()[[1L]]), x, y))
if (...length()) str(list(...))
}
foo.C <- function(x, y = 3, ...) {
cat(sprintf("%s: x=%s, y=%s\n", as.character(match.call()[[1L]]), x, y))
if (...length()) str(list(...))
NextMethod("foo", x = x, y = y)
}
c <- structure(class = "C", 1)
# 'x' winds up in ..1
foo(c)
# foo.C: x=1, y=3
# foo.default: x=1, y=3
# List of 1
# $ : 'C' num 1
# empty ...!
foo(x=c)
# foo.C: x=1, y=3
# foo.default: x=1, y=3
# now both x is ..1, y is ..2
foo(c, 4)
# foo.C: x=1, y=4
# foo.default: x=1, y=4
# List of 2
# $ : 'C' num 1
# $ : num 4
# perhaps predictably, ...length()==0
foo(x=c, y=4)
# foo.C: x=1, y=4
# foo.default: x=1, y=4
I've tried re-reading ?NextMethod a few times as well as R-lang [1] &
can't make heads or tails of this. I've also come across related 2012
(!) thread [2] and tangentially-related bug [3].
Is this intended behavior? If so, might I reiterate Henrik's long-ago
request for better documentation of how to work around this?
For some added context, where I actually encountered this, my S3
method is mainly written to overwrite the defaults of a parent class's
method.
Mike C
[1] https://cran.r-project.org/doc/manuals/r-devel/R-lang.html#NextMethod
[2] https://stat.ethz.ch/pipermail/r-devel/2012-October/065016.html
[3] https://bugs.r-project.org/show_bug.cgi?id=15654
More information about the R-devel
mailing list