| 54325 |
jmc |
1 |
## reset inherited methods of group members
|
|
|
2 |
## (contributed by Martin Morgan, 2011-2-9)
|
|
|
3 |
setClass("A", representation("numeric"))
|
|
|
4 |
a <- new("A")
|
|
|
5 |
|
|
|
6 |
setMethod("Logic", c("A", "A"), function(e1, e2) FALSE)
|
| 89300 |
smeyer |
7 |
stopifnot(isFALSE(a & a)) # inherit &,A,A-method
|
| 54325 |
jmc |
8 |
setMethod("Logic", c("A", "A"), function(e1, e2) TRUE)
|
| 89300 |
smeyer |
9 |
stopifnot(isTRUE(a & a))
|
| 88033 |
maechler |
10 |
## feature in R >= 4.5.0: these get same method, but slightly differing result:
|
|
|
11 |
(sa <- selectMethod("&", c("A", "A"))) # printing "Generic: ..."
|
|
|
12 |
(sL <- selectMethod("Logic", c("A", "A"))) #
|
|
|
13 |
stopifnot(identical(sa@generic,
|
|
|
14 |
structure("Logic", package = "base",
|
|
|
15 |
orig = structure("&", package = "base"))))
|
| 54325 |
jmc |
16 |
|
|
|
17 |
removeMethod("Logic", c("A", "A"))
|
| 89300 |
smeyer |
18 |
stopifnot(identical(logical(), a & a))
|
| 54325 |
jmc |
19 |
|
|
|
20 |
removeClass("A")
|
| 58493 |
maechler |
21 |
|
|
|
22 |
### Find inherited group methods:
|
| 70274 |
maechler |
23 |
if(require(Matrix)) { ## , lib.loc = .Library
|
| 69213 |
ripley |
24 |
sm <- selectMethod("-", c("dgCMatrix", "numeric"))# direct match with "Arith"
|
|
|
25 |
s2 <- selectMethod("-", c("dtCMatrix", "numeric"))# ambiguity match with "Arith"
|
|
|
26 |
stopifnot(sm@generic == "Arith", s2@generic == "Arith")
|
|
|
27 |
}
|
| 58511 |
maechler |
28 |
## was not ok in R 2.14.x
|
| 58493 |
maechler |
29 |
|
| 88267 |
maechler |
30 |
## some tests of callGeneric(). It's recommended for use with group generics
|
| 66423 |
jmc |
31 |
setGeneric("f1", signature=c("a"),
|
|
|
32 |
function(..., a) standardGeneric("f1"))
|
|
|
33 |
setMethod("f1", c(a="ANY"), function(..., a) list(a=a, ...))
|
|
|
34 |
setMethod("f1", c(a="missing"), function(..., a) callGeneric(a=1, ...))
|
|
|
35 |
f2 <- function(b,c,d, a) {
|
|
|
36 |
if (missing(a))
|
|
|
37 |
f1(b=b, c=c, d=d)
|
|
|
38 |
else
|
|
|
39 |
f1(a=a, b=b, c=c, d=d)
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
## use callGeneric both directly (f1) and indirectly (f2)
|
|
|
43 |
## Latter failed pre rev. 66408; Bug ID 15937
|
| 66586 |
maechler |
44 |
stopifnot(identical(c(1,2,3,4), as.vector(unlist(f1(2,3,4)))))
|
|
|
45 |
stopifnot(identical(c(1,2,3,4), as.vector(unlist(f2(2,3,4)))))
|
| 66423 |
jmc |
46 |
|
|
|
47 |
## test callGeneric() with no arguments. This is rarely used
|
|
|
48 |
## because nearly all applications use the groups Ops, etc.
|
|
|
49 |
## whose members are primitives => must supply args to callGeneric
|
|
|
50 |
|
|
|
51 |
Hide <- setClass("Hide", slots = c(data = "vector"), contains = "vector")
|
|
|
52 |
|
|
|
53 |
unhide <- function(obj)
|
|
|
54 |
obj@data
|
|
|
55 |
|
|
|
56 |
setGeneric("%p%", function(e1, e2) e1 + e2, group = "Ops2")
|
|
|
57 |
setGeneric("%gt%", function(e1, e2) e1 > e2, group = "Ops2")
|
|
|
58 |
|
|
|
59 |
setGroupGeneric("Ops2", function(e1,e2)NULL, knownMembers = c("%p%","%gt%"))
|
|
|
60 |
|
|
|
61 |
setMethod("Ops2", c("Hide", "Hide"),
|
|
|
62 |
function(e1, e2) {
|
|
|
63 |
e1 <- unhide(e1)
|
|
|
64 |
e2 <- unhide(e2)
|
|
|
65 |
callGeneric()
|
|
|
66 |
})
|
|
|
67 |
|
|
|
68 |
setMethod("Ops2", c("Hide", "vector"),
|
|
|
69 |
function(e1, e2) {
|
|
|
70 |
e1 <- unhide(e1)
|
|
|
71 |
callGeneric()
|
|
|
72 |
})
|
|
|
73 |
setMethod("Ops2", c("vector", "Hide"),
|
|
|
74 |
function(e1, e2) {
|
|
|
75 |
e2 <- unhide(e2)
|
|
|
76 |
callGeneric()
|
|
|
77 |
})
|
|
|
78 |
|
|
|
79 |
h1 <- Hide(data = 1:10)
|
|
|
80 |
h2 <- Hide(data = (1:10)*.5+ 0.5)
|
|
|
81 |
|
|
|
82 |
stopifnot(all.equal(h1%p%h2, h1@data + h2@data))
|
|
|
83 |
stopifnot(all.equal(h1 %gt% h2, h1@data > h2@data))
|
|
|
84 |
|
|
|
85 |
removeClass("Hide")
|
|
|
86 |
for(g in c("f1", "%p%", "%gt%", "Ops2"))
|
|
|
87 |
removeGeneric(g)
|
|
|
88 |
|
|
|
89 |
|