From p@ve| @end|ng |rom uow@edu@@u  Sun May 19 23:23:45 2019
From: p@ve| @end|ng |rom uow@edu@@u (Pavel Krivitsky)
Date: Sun, 19 May 2019 21:23:45 +0000
Subject: [Rd] [R-pkg-devel] Three-argument S3method declaration does not
 seem to affect dispatching from inside the package.
In-Reply-To: <CALEXWq0duRo9zX1f_TBytsEZgdfhtxvQ=kFKtnzZP-sZOp6iXQ@mail.gmail.com>
References: <49d473a1bfa29f95d74f931ecfdc2a6b346ca6a2.camel@uow.edu.au>
 <CALEXWq058Wg0oskeaFAjPdptTEjVMWLFbUr+FeBwdKwP93T=Kw@mail.gmail.com>
 <14d531d671791b51683b71ee0eece1c9a29c374d.camel@uow.edu.au>
 <CALEXWq02kZdw79BWJiAtj-HXMnCeX_PRF_yFTT6DpNCn=MxPAw@mail.gmail.com>
 <8429787eb123a937ddb8770c80587d6e2ae05195.camel@uow.edu.au>
 <CALEXWq0duRo9zX1f_TBytsEZgdfhtxvQ=kFKtnzZP-sZOp6iXQ@mail.gmail.com>
Message-ID: <133e2cf6d1a5a3597545997f441aca9502597632.camel@uow.edu.au>

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
namespace.

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.

				Best,
				Pavel

-- 
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.


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