Skip to content
Prev 5620 / 12125 Next

[R-pkg-devel] [External] Re: Two packages with the same generic function

Thanks to all who replied and provided input.

@Duncan: Yes, of course there is the note about masking. In my experience, many people won't understand what this means, what the consequences are, and that pkgA::foo(x) and pkgB::foo(x) are workarounds. And yes, pkgA importing the foo() generic from pkgB or vice-versa is an obvious solution.

@Bert: Related to the suggestion that one package could import the generic from the other, you raise an important point. This in essence adds a whole package as a dependency for these two lines of code:

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

And let's say pkgA imports this from pkgB and pkgB depends on 10 other packages? Does this mean that pkgA has now added 11 packages as dependencies for importing 2 lines of code? If so, that seems quite undesirable to me.

@Neal: A separate package with generic functions that pkgA and pkgB could import is an interesting suggestion, thanks!

@Tom: I might need some time to process what you are suggesting. What I am really hoping for (and not sure if your approach can handle this) is that the following works regardless of the order in which pkgA and pkgB are loaded:

x <- 1:4
class(x) <- "A"
foo(x)
class(x) <- "B"
foo(x)

Again, that used to be the case in R 3.6.3, but not anymore in 4.0.x.

Best,
Wolfgang