Closed calderonsamuel closed 8 months ago
print()
is an S3 generic so doesn't understand how to use super()
. In this case, I think the right action is to get the underlying S3 object out of your class:
method(print, foo_S7) <- function(x) {
cli::cli_text("This is a foo_S7 object")
print(S7_data(x))
}
We need to document this better.
Using NextMethod()
worked for me. Is using S7_data()
always a better alternative?
method(print, foo_S7) <- function(x) {
cli::cli_text("This is a foo_S7 object")
NextMethod("print", x)
}
Hmmmmmmmmmmmmmmmmmmm.
NextMethod()
works because this is an S3 method, and so I think it's fine. But it's problematic for the same reasons that NextMethod()
is problematic in S3 code: it's not obvious exactly what method is going to be called. So I think using S7_data()
is a bit clearer here, but I don't feel strongly about it.
I want a custom print method for a child class of
data.frame
. This is a toy example of the class deffinition.I print some metadata and use
NextMethod()
to print the data.frame.While trying to do the same with S7, the class deffinition works ok.
Following the examples on the
super()
help page, I would expect to define the print method like this, but it looks likesuper()
's own method takes precedence.Eventually, I would like to inherit from tibble and not from data.frame, which I'm finding very difficult to do, but that will be a separate issue.
Created on 2023-09-19 with reprex v2.0.2