Skip to content

S4 Generics and NAMESPACE : justified warning ?

2 messages · Yohan Chalabi, Martin Maechler

#
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,
9 days later
#
>>>>> "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.  The question
    MM> is: is there a patch which does differentiate between
    MM> your situation and Martin's scenario -- or does your
    MM> patch already do that?


    YC> Hi Martin,

    YC> Thanks for your response.

    YC> Here is an attempt to combine both views.

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

    YC> regards, Yohan

    [.....]

I have now committed your proposal (+-),
and also added <R>/tests/Pkgs/ in order to allow checking such 
Namespace <-> S4 and other package dependencies.

Martin Maechler, ETH Zurich