doc: method and class definition order matters
documentation
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
```r
Todo <- S7::new_class(
"Todo",
properties = list(
...
),
)
```
In `data-frame-methods.R` I have a method defined:
```r
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](https://roxygen2.r-lib.org/articles/collate.html) 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"](https://rconsortium.github.io/S7/articles/generics-methods.html)?
关闭于 2026-05-21 3 条评论