forked from RConsortium/S7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial.R
61 lines (56 loc) · 1.45 KB
/
special.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
49
50
51
52
53
54
55
56
57
58
59
60
61
#' Dispatch on a missing argument
#'
#' Use `class_missing` to dispatch when the user has not supplied an argument,
#' i.e. it's missing in the sense of [missing()], not in the sense of
#' [is.na()].
#'
#' @export
#' @return Sentinel objects used for special types of dispatch.
#' @format NULL
#' @examples
#' foo <- new_generic("foo", "x")
#' method(foo, class_numeric) <- function(x) "number"
#' method(foo, class_missing) <- function(x) "missing"
#' method(foo, class_any) <- function(x) "fallback"
#'
#' foo(1)
#' foo()
#' foo("")
class_missing <- structure(list(), class = "S7_missing")
is_class_missing <- function(x) inherits(x, "S7_missing")
#' @export
print.S7_missing <- function(x, ...) {
cat("<S7_missing>\n")
invisible(x)
}
#' @export
str.S7_missing <- function(object, ..., nest.lev = 0) {
cat(if (nest.lev > 0) " ")
print(object)
}
#' Dispatch on any class
#'
#' Use `class_any` to register a default method that is called when no other
#' methods are matched.
#'
#' @export
#' @format NULL
#' @examples
#' foo <- new_generic("foo", "x")
#' method(foo, class_numeric) <- function(x) "number"
#' method(foo, class_any) <- function(x) "fallback"
#'
#' foo(1)
#' foo("x")
class_any <- structure(list(), class = "S7_any")
is_class_any <- function(x) inherits(x, "S7_any")
#' @export
print.S7_any <- function(x, ...) {
cat("<S7_any>\n")
invisible(x)
}
#' @export
str.S7_any <- function(object, ..., nest.lev = 0) {
cat(if (nest.lev > 0) " ")
print(object)
}