From chalabi at phys.ethz.ch  Thu Sep  3 12:44:13 2009
From: chalabi at phys.ethz.ch (Yohan Chalabi)
Date: Thu, 3 Sep 2009 12:44:13 +0200
Subject: [Rd] S4 Generics and NAMESPACE : justified warning ?
In-Reply-To: <19099.51012.936944.591938@lynne.math.ethz.ch>
References: <20090817135120.16fab862@mimi>
	<30312_1250600886_4A8AA7B5_30312_581_1_4A8AA986.3040702@fhcrc.org>
	<20090828162429.32576c07@mimi>
	<19099.51012.936944.591938@lynne.math.ethz.ch>
Message-ID: <20090903124413.10c3763c@mimi>

>>>> "MM" == Martin Maechler <maechler at stat.math.ethz.ch>
>>>> on Mon, 31 Aug 2009 14:51:16 +0200

   MM> Yes, I agree, unjustified in your case.
   MM> The question is: is there a patch which does differentiate
   MM> between your situation and Martin's scenario -- or does your
   MM> patch already do that?


Hi Martin,

Thanks for your response.

Here is an attempt to combine both views. 

To check if the generic was derived from the function it wants to
overwrite, the following patch checks if the package associated with
the generic is the same as the one where the function was defined.

regards,
Yohan

Index: src/library/base/R/namespace.R
===================================================================
--- src/library/base/R/namespace.R	(revision 49543)
+++ src/library/base/R/namespace.R	(working copy)
@@ -797,8 +797,19 @@
 	}
     }
     for (n in impnames)
-        if (exists(n, envir = impenv, inherits = FALSE))
-            warning(msg, " ", n)
+        if (exists(n, envir = impenv, inherits = FALSE)) {
+            if (.isMethodsDispatchOn() && methods:::isGeneric(n, ns)) {
+                ## warning if generic overwrites a function which
+                ## it was not derived from
+                genNs <- methods:::slot(get(n, envir = ns), "package")
+                genImpenv <- environmentName(environment(get(n, envir = impenv)))
+                genWarn1 <- (!identical(genNs, genImpenv))
+                ## warning if generic overwrites another generic
+                genWarn2 <- methods:::isGeneric(n, impenv)
+                if (genWarn1 || genWarn2)
+                    warning(msg, " ", n)
+            } else warning(msg, " ", n)
+        }
     importIntoEnv(impenv, impnames, ns, impvars)
     if (register) {
         addImports(self, ns,



[Rd] S4 Generics and NAMESPACE : justified warning ?
Yohan Chalabi