Skip to content

Defining generic methods for suggested packages #573

@jtlandis

Description

@jtlandis

Hello everyone,

When building a package that is exporting a generic function, what is the proper way to export a method for a class within a "Suggests" package?

For example, if package foo exports the foo() generic, but lists package bar as a Suggests, what would be the proper way to register methods for classes within bar?

Would it be something to the effect of:

# from the foo package
.onLoad <- function(lib, pkg) {

  if (isNamespaceLoaded("bar")) {
    method(foo, bar::class_bar) <- function(x) {cat("we dispatched on `bar::class_bar`")}
  } else {
    setHook(packageEvent("bar", "onLoad"), function(){
      method(foo, bar::class_bar) <- function(x) {cat("we dispatched on `bar::class_bar`")}
    })
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions