Repository navigation
Expand file tree
/
Copy pathdispatch.R
More file actions
138 lines (121 loc) · 3.36 KB
/
Copy pathdispatch.R
File metadata and controls
138 lines (121 loc) · 3.36 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#' Illustrate S3 dispatch
#'
#' @description
#' `s3_dispatch()` prints a list of all possible function names that will be
#' considered for method dispatch. There are four possible states:
#'
#' * `=>` method exists and is found by `UseMethod()`.
#' * `->` method exists and is used by `NextMethod()`.
#' * `*` method exists but is not used.
#' * Nothing (and greyed out in console): method does not exist.
#'
#' Learn more at <https://adv-r.hadley.nz/s3.html>.
#'
#' @param call Example call to S3 method
#' @param env Environment in which to evaluate call
#' @export
#' @examples
#' x <- Sys.time()
#' s3_dispatch(print(x))
#' s3_dispatch(is.numeric(x))
#' s3_dispatch(as.Date(x))
#' s3_dispatch(sum(x))
#'
#' # Internal vs. regular generic
#' x1 <- 1
#' x2 <- structure(2, class = "double")
#'
#' my_length <- function(x) UseMethod("my_length")
#' s3_dispatch(my_length(x1))
#' s3_dispatch(my_length(x2))
#'
#' length.double <- function(x) 10
#' s3_dispatch(length(x1))
#' s3_dispatch(length(x2))
s3_dispatch <- function(call, env = parent.frame()) {
call <- enexpr(call)
if (!is_call(call)) {
stop("`call` must be a function call", call. = FALSE)
}
f <- call[[1]]
if (is_call(f)) {
n <- length(f)
f <- f[[n]]
}
generic <- as.character(f)
x <- eval(call[[2]], env)
class <- c(s3_class(x), "default")
names <- paste0(generic, ".", class)
impls <- method_find(generic, class, env = env)
# Add group generic if necssary
group <- find_group(generic)
if (!is.null(group)) {
names <- c(names, paste0(group, ".", class))
impls <- c(impls, method_find(group, class, env = env))
}
# internal generics can resolve to internal method
if (is_internal_generic(generic)) {
internal <- !is.object(x)
names <- c(names, paste0(generic, " (internal)"))
impls <- c(impls, get(generic))
} else {
internal <- FALSE
}
structure(
list(
method = names,
impl = impls,
exists = !purrr::map_lgl(impls, is.null),
to_next = purrr::map_lgl(impls, calls_next_method)
),
internal = internal,
class = "method_table"
)
}
calls_next_method <- function(f) {
if (is.primitive(f) || is.null(f)) {
FALSE
} else {
uses <- codetools::findGlobals(f, merge = FALSE)$functions
any(uses == "NextMethod")
}
}
method_find <- function(generic, class, env = parent.frame()) {
purrr::map2(generic, class, utils::getS3method, envir = env, optional = TRUE)
}
#' @export
print.method_table <- function(x, ...) {
if (attr(x, "internal")) {
bullet <- ifelse(x$exists, " *", " ")
bullet[[length(bullet)]] <- "=>"
} else {
first <- TRUE
to_next <- TRUE
bullet <- character(length(x$exists))
for (i in seq_along(x$exists)) {
if (!x$exists[[i]]) {
bullet[[i]] <- " "
} else {
if (first) {
bullet[[i]] <- "=>"
first <- FALSE
} else if (to_next) {
bullet[[i]] <- "->"
} else {
bullet[[i]] <- " *"
}
to_next <- (to_next || first) && x$to_next[[i]]
}
}
}
method <- ifelse(x$exists, x$method, crayon::silver(x$method))
cat(paste0(bullet, " ", method, "\n", collapse = ""), sep = "")
invisible(x)
}
find_group <- function(generic) {
g <- group_generics()
g_table <- stats::setNames(rep(names(g), lengths(g)), unlist(g))
if (!generic %in% names(g_table))
return()
g_table[[generic]]
}