The R Project SVN R

Rev

Rev 52846 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
52816 jmc 1
setRefClass("foo", list(bar = "numeric", flag = "character"),
2
            fieldPrototypes = list(flag = "standard flag"),
3
            classMethods = list(
4
            addToBar = function(incr) {
5
                b = getBar() + incr
6
                setBar(b)
7
                b
8
            }
9
            ))
10
ff = new("foo", bar = 1.5)
11
stopifnot(identical(ff$bar, 1.5))
12
ff$bar <- pi
13
stopifnot(identical(ff$bar, pi))
14
 
15
stopifnot(identical(ff$flag, "standard flag"))
16
 
17
ff$setBar(1:3)
18
stopifnot(identical(ff$bar, 1:3))
19
 
20
ff$getBar()
21
stopifnot(all.equal(ff$addToBar(1), 2:4))
22
 
23
 
24
## inheritance.  redefines flag
25
setRefClass("foo2", list(b2 = "numeric", flag = "complex"),
26
            contains = "foo",
27
            classMethods = list(addBoth = function(incr) {
28
                addToBar(incr) #uses inherited class method
29
                setB2(getB2() + incr)
30
                }))
31
f2 <- new("foo2", bar = -3, flag = 1+1i, b2 = ff$bar)
32
stopifnot(identical(f2$flag, 1+1i), identical(f2$bar, -3),
33
          all.equal(f2$b2, 2:4+0))
34
f2$addBoth(-1)
35
stopifnot(all.equal(f2$bar, -4), all.equal(f2$b2, 1:3+0))
36
 
37
setRefClass("foo3", contains = "foo2",
38
            classMethods = list(addBoth = function(incr) {
39
                addBoth.prev(incr)
40
                setFlag(getFlag()+incr)
41
                incr
42
            }))
43
 
44
## this should be f3 <- as(f2, "foo3")
45
f3 <- new("foo3", bar = f2$bar, b2 = f2$b2, flag = f2$flag)
46
f3$addBoth(1)
47
stopifnot(all.equal(f3$bar, -3), all.equal(f3$b2, 2:4+0),
48
          all.equal(f3$flag, 2+1i))