chooseOpsMethod {base} | R Documentation |
Choose the Appropriate Method for Ops
Description
chooseOpsMethod
is a function called by the Ops
Group Generic when two
suitable methods are found for a given call. It determines which method to
use for the operation based on the objects being dispatched.
The function is first called with reverse = FALSE
, where
x
corresponds to the first argument and y
to the second
argument of the group generic call. If chooseOpsMethod()
returns
FALSE
for x
, then chooseOpsMethod
is called again,
with x
and y
swapped, mx
and my
swapped,
and reverse = TRUE
.
Usage
chooseOpsMethod(x, y, mx, my, cl, reverse)
Arguments
x , y |
the objects being dispatched on by the group generic. |
mx , my |
the methods found for objects |
cl |
the call to the group generic. |
reverse |
logical value indicating whether |
Value
This function must return either TRUE
or FALSE
. A value of
TRUE
indicates that method mx
should be used.
See Also
Examples
# Create two objects with custom Ops methods
foo_obj <- structure(1, class = "foo")
bar_obj <- structure(1, class = "bar")
`+.foo` <- function(e1, e2) "foo"
Ops.bar <- function(e1, e2) "bar"
invisible(foo_obj + bar_obj) # Warning: Incompatible methods
chooseOpsMethod.bar <- function(x, y, mx, my, cl, reverse) TRUE
stopifnot(exprs = {
identical(foo_obj + bar_obj, "bar")
identical(bar_obj + foo_obj, "bar")
})
# cleanup
rm(foo_obj, bar_obj, `+.foo`, Ops.bar, chooseOpsMethod.bar)