Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 63 lines (48 sloc) 1.612 kB
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
1 #### Testing UseMethod() and even more NextMethod()
ffccefb comment
maechler authored
2 #### --------------------
3 #### i.e., S3 methods *only*. For S4, see reg-S4.R
4 ## ~~~~~~~~
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
5
6 ###-- Group methods
7
fb8f328 example demonstrated an auto-printing bug
ripley authored
8 ## previous versions used print() and hit an auto-printing bug.
9
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
10 ### Arithmetic "Ops" :
fb8f328 example demonstrated an auto-printing bug
ripley authored
11 ">.bar" <- function(...) {cat("using >.bar\n"); FALSE}
12 ">.foo" <- function(...) {cat("using >.foo\n"); TRUE}
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
13 Ops.foo <- function(...) {
fb8f328 example demonstrated an auto-printing bug
ripley authored
14 cat("using Ops.foo\n")
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
15 NextMethod()
16 }
fb8f328 example demonstrated an auto-printing bug
ripley authored
17 Ops.bar <- function(...) {
18 cat("using Ops.bar\n")
19 TRUE
20 }
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
21
22 x <- 2:4 ; class(x) <- c("foo", "bar")
23 y <- 4:2 ; class(y) <- c("bar", "foo")
24
25 ## The next 4 give a warning each about incompatible methods:
26 x > y
27 y < x # should be the same (warning msg not, however)
28 x == y
29 x <= y
30
31 x > 3 ##[1] ">.foo"
32
33 rm(list=">.foo")
34 x > 3 #-> "Ops.foo" and ">.bar"
35
36
37
38 ### ------------ was ./mode-methods.R till R ver. 1.0.x ----------------
39
40 ###-- Using Method Dispatch on "mode" etc :
d28d25a methods package included at startup by default;
jmc authored
41 ## Tests S3 dispatch with the class attr forced to be data.class
42 ## Not very relevant when S4 methods are around, but kept for historical interest
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
43 abc <- function(x, ...) {
e8553e5 tidy up example now all objects have a class
ripley authored
44 cat("abc: Before dispatching; x has class `", class(x), "':", sep="")
45 str(x)
dff673e incorrect UseMethod call
ripley authored
46 UseMethod("abc", x) ## UseMethod("abc") (as in S) fails
9bd0178 method-dispatch instead (and encompassing) mode-methods
maechler authored
47 }
48
49 abc.default <- function(x, ...) sys.call()
50
51 "abc.(" <- function(x)
52 cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n")
53 abc.expression <- function(x)
54 cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n")
55
56 abc(1)
57 e0 <- expression((x))
58 e1 <- expression(sin(x))
59 abc(e0)
60 abc(e1)
61 abc(e0[[1]])
62 abc(e1[[1]])
Something went wrong with that request. Please try again.