Skip to content

doc: method and class definition order matters #588

@JosiahParry

Description

@JosiahParry

When defining S7 classes and methods, the order matters whereas typically this is not the case.

For example, in types.R I have a Todo definition

Todo <- S7::new_class(
  "Todo",
  properties = list(
 ...
  ),
)

In data-frame-methods.R I have a method defined:

method(as.data.frame, Todo) <- function(x, ...) {
  data.frame(
     ...
  )
}

This causes an error when using devtools::load_all()

Error in `load_all()`:
! Failed to load R/data-frame-methods.R
Caused by error:
! object 'Todo' not found

I believe this is because of the collation order putting data-frame-methods.R before types.R. The fix is to either change the file names or to put the method definition after the class definition.

Perhaps recommendations can be made in "Generics and methods"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions