[Rd] [R-pkg-devel] Three-argument S3method declaration does not seem to affect dispatching from inside the package.

Pavel Krivitsky p@ve| @end|ng |rom uow@edu@@u
Sun May 19 23:23:45 CEST 2019

Hi, Inaki,

On Sun, 2019-05-19 at 16:59 +0200, Iñaki Ucar wrote:
> IMO the simplest way to do this is to check who the caller was:
> foo <- function(x) UseMethod("foo")
> foo.bar <- function(x) {
>   sc <- sys.call(-1)
>   if (is.null(sc) || sc[[1]] != "foo")
>     .Deprecated(msg="Calling 'foo.bar' directly is deprecated")
> }
> x <- 1
> class(x) <- "bar"
> foo(x)      # silent
> foo.bar(x)  # a warning is issued

f <- getS3method("foo","bar")
f(x) # spurious warning

foo.baz <- function(x) NextMethod("foo")
class(x) <- c("baz","bar")
foo(x) # spurious warning

Believe me, I spent a lot of time trying to get this to work, and I
tried even more sophisticated call stack alchemy, but people kept
getting false positives and negatives. (Take a look at my attempt in
the statnet.common package.)

> The description in the documentation means that point 3) in your list
> goes always first, which automatically implies 2) if the generic is
> defined in the same package.

Are you sure which package defines the generic matters? I've just ran
some tests with two packages and moving the generic around doesn't seem
to affect things: the calling function determines whose method is used.

It seems to me like there is no contradiction after all, except that I
propose that the registered method should take precedence within a

The only situation in which it would change R's behaviour would be when
a package/namespace contains a function foo.bar() AND a NAMESPACE
containing S3method(foo,bar,not.foo.bar) AND calls foo() on objects of
type bar from inside the package. It is extremely unlikely to break any
existing code.


Pavel Krivitsky
Lecturer in Statistics
National Institute of Applied Statistics Research Australia (NIASRA)
School of Mathematics and Applied Statistics | Building 39C Room 154
University of Wollongong NSW 2522 Australia
T +61 2 4221 3713
Web (NIASRA): http://niasra.uow.edu.au/index.html
Web (Personal): http://www.krivitsky.net/research
ORCID: 0000-0002-9101-3362

NOTICE: This email is intended for the addressee named and may contain
confidential information. If you are not the intended recipient, please
delete it and notify the sender. Please consider the environment before
printing this email.

More information about the R-devel mailing list