Skip to content
Back to formatted view

Raw Message

Message-ID: <d5c86a89b6f541e495f44efbb7e6e4c4@UM-MAIL3214.unimaas.nl>
Date: 2020-06-22T17:00:02Z
From: Wolfgang Viechtbauer
Subject: [R-pkg-devel] Two packages with the same generic function

Hi All,

Let's say there are two packages pkgA and pkgB, both of which have a generic function

foo <- function(x, ...)
   UseMethod("foo")

and pkgA has a method for objects of class "A":

foo.A <- function(x, ...)
   print(x)

and pkgB has a method for objects of class "B":

foo.B <- function(x, ...)
   plot(x)

Both packages export foo and their method and declare their respective S3 methods, so:

export(foo)
export(foo.A)
S3method(foo, A)

in NAMESPACE of pkgA and

export(foo)
export(foo.B)
S3method(foo, B)

in NAMESPACE of pkgB.

If a user loads pkgA first and then pkgB, this fails:

library(pkgA)
library(pkgB)
x <- 1:4
class(x) <- "A"
foo(x)

Error in UseMethod("foo") : 
  no applicable method for 'foo' applied to an object of class "A"

and vice-versa. Of course, pkgA::foo(x) works. Aside from pkgA importing foo() or vice-versa, is there some other clever way to make this work? In earlier versions of R (at least in 3.6.3), this used to work (i.e., the generic foo() from pkgB would find method foo.A() and vice-versa), but not since 4.0.0.

Best,
Wolfgang