Is there a way to get the actual class name being used when calling a S3 method?
For example, is there a way to have foo.bar() always print Call method for class: bar independently of the order (and without knowing other class names) of the classes defined in structure()?
foo <- function(x) {
UseMethod("foo")
}
foo.bar <- function(x) {
cat("Call method for class:", class(x)[[2]])
}
x <- structure("x", class = c("baz", "bar", "character"))
foo(x)
In my case, I've an object that gets new classes in the building process. This is not something I usually do but it makes sense in my case I guess. I can easily get around my issue by changing my design a bit or by appending (instead of prepending) the new classes so that the first class of my object is the one I want to display.