Skip to content

How to perform a two-step method dispatch? #589

@niheaven

Description

@niheaven

Hey there!

I'm trying to set up a "two-step" method dispatch in S7, I'd like my generic function to automatically pick different versions depending on the classes of the arguments, and each version should accept slightly different parameter names.

For example:

method(fun, list(class_character, class_integer, class_numeric)) <- function(x, int_1, num_1) { ... }
method(fun, list(class_numeric, class_integer, class_character)) <- function(x, int_2, chr_1) { ... }

Then, when I call it, the right version kicks in based on the order of classes—and I'd use the matching parameter names:

fun(x, int_1 = xxx, num_1 = xxx)  # x is character, and uses the first version
fun(x, int_2 = xxx, chr_1 = xxx)  # x is numeric, and uses the second version

Any idea if this is possible? And if so, what's the cleanest way to pull it off?

Thanks!

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