From p@ve| @end|ng |rom uow@edu@@u  Tue May 14 12:31:40 2019
From: p@ve| @end|ng |rom uow@edu@@u (Pavel Krivitsky)
Date: Tue, 14 May 2019 10:31:40 +0000
Subject: [Rd] [R-pkg-devel] Three-argument S3method declaration does not
 seem to affect dispatching from inside the package.
In-Reply-To: <CALEXWq058Wg0oskeaFAjPdptTEjVMWLFbUr+FeBwdKwP93T=Kw@mail.gmail.com>
References: <49d473a1bfa29f95d74f931ecfdc2a6b346ca6a2.camel@uow.edu.au>
 <CALEXWq058Wg0oskeaFAjPdptTEjVMWLFbUr+FeBwdKwP93T=Kw@mail.gmail.com>
Message-ID: <14d531d671791b51683b71ee0eece1c9a29c374d.camel@uow.edu.au>

Hi, I?aki,

Thanks for looking into this.

On Tue, 2019-05-14 at 11:33 +0200, I?aki Ucar wrote:
> > So, calling the same generic function with the same class results
> > in different dispatching behaviour depending on whether the call is
> > from within the package doing the export or from the outside.
> 
> It does not depend on whether you export gen.formula() or not. When
> you call gen() inside your package, the S3 dispatch mechanism finds a
> method gen.formula defined in that environment (the package's
> namespace), so it is called.

Indeed, this is the case. The issue is that when both are available and
exported, S3method takes precedence outside the package but function
name takes precedence inside the package.

> Note that disabling name-based dispatch implies two things: 1) the
> inability to override your method by defining gen.formula in the
> global environment, and 2) another package can break yours (i.e.,
> internal calls to gen()) by registering an S3 method for gen() after
> you.

That's a good point.

   > library(anRpackage)
   > gen(a~b)
   I am the S3method-declared method.
   > gen.formula <- function(object, ...){message("I am the externally declared method.")}
   > gen(a~b)
   I am the externally declared method.
   > test_me()
   I am the tester. Which one will I call?
   I am the function with an unfortunate name.

In that case, I think that the least surprising behaviour would
prioritise declarations and methods "nearer" to the caller over those
"farther" from the caller (where "caller" is the caller of the generic,
not the generic itself), and, within that, give precedence to S3method
declarations over function names.

That is, for a call from inside a package, the order of precedence
would be as follows:
   1. S3method() in that package's NAMESPACE.
   2. Appropriately named function?in that package (exported or not).
   3. Appropriately named function in calling environment (which may be
      GlobalEnv).
   4. S3method() in other loaded packages' NAMESPACEs.
   5. Appropriately named functions exported by other loaded packages'
      NAMESPACEs.

For a call from outside a package, the precedence is the same, but 1
and 2 are not relevant.

As far as I can tell, this is the current behaviour except for the
relative ordering of 1 and 2.

				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.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: anRpackage_1.0.tar.gz
Type: application/x-compressed-tar
Size: 750 bytes
Desc: anRpackage_1.0.tar.gz
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190514/c7e72e56/attachment.bin>


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