[Rd] S4 Generics and NAMESPACE : justified warning ?

Yohan Chalabi chalabi at phys.ethz.ch
Thu Sep 3 12:44:13 CEST 2009


>>>> "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,



More information about the R-devel mailing list