-
Notifications
You must be signed in to change notification settings - Fork 41
Description
I'm currently trying to mimic the behavior of convert()
, where an argument is interpreted literally as a class for dispatch. From what I can tell, this isn't possible currently -- at least not in as rigorously as convert
. The best I can come up with is to catch all the calls and then dispatch on a specialized S3 class names. But this leaves quite a lot to be desired.
Nevertheless, I think the current implementation is nearly capable of this type of dispatch.
To support this, I think a minor change to method
would make this feasible. method()
currently accepts a signature from class
es, or a signature from object
s. To implement non-standard dispatch like we see in convert()
, we'd want a mix of class
and object
arguments.
If this was allowed, I think convert
could even leverage this instead of requiring its own specialized internal method_
to handle this dispatch.
convert <- new_generic("convert", c("from", "to"))
method(convert, list(class_any, class_any)) <- function(from, to, ...) {
# non-standard dispatch, using to argument as class for dispatch
dispatched_method <- method(
convert,
class = list(to = to), # use `to` class for dispatch
object = list(from = from) # but `from` object
)
dispatched_method(from, to, ...)
}
Of course the exact syntax is up for debate, but hopefully this communicates the idea. This would allow us to pick up the class to
and use it directly for dispatch, while using the object, from
.