forked from RConsortium/S7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod-ops.R
48 lines (40 loc) · 1.26 KB
/
method-ops.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
base_ops <- NULL
base_matrix_ops <- NULL
on_load_define_ops <- function() {
base_ops <<- lapply(
setNames(, group_generics()$Ops),
new_generic,
dispatch_args = c("e1", "e2")
)
base_matrix_ops <<- lapply(
setNames(, group_generics()$matrixOps),
new_generic,
dispatch_args = c("x", "y")
)
}
#' @export
Ops.S7_object <- function(e1, e2) {
cnd <- tryCatch(
return(base_ops[[.Generic]](e1, e2)),
S7_error_method_not_found = function(cnd) cnd
)
if (S7_inherits(e1) && S7_inherits(e2)) {
stop(cnd)
} else {
# Must call NextMethod() directly in the method, not wrapped in an
# anonymous function.
NextMethod()
}
}
#' @rawNamespace if (getRversion() >= "4.3.0") S3method(chooseOpsMethod, S7_object)
chooseOpsMethod.S7_object <- function(x, y, mx, my, cl, reverse) TRUE
#' @rawNamespace if (getRversion() >= "4.3.0") S3method(matrixOps, S7_object)
matrixOps.S7_object <- function(x, y) {
base_matrix_ops[[.Generic]](x, y)
}
#' @export
Ops.S7_super <- Ops.S7_object
#' @rawNamespace if (getRversion() >= "4.3.0") S3method(chooseOpsMethod, S7_super)
chooseOpsMethod.S7_super <- chooseOpsMethod.S7_object
#' @rawNamespace if (getRversion() >= "4.3.0") S3method(matrixOps, S7_super)
matrixOps.S7_super <- matrixOps.S7_object