defineRefClass: Define a Reference Class

Description Usage Arguments See Also Examples

Description

This is a wrapper around setRefClass. All arguments are defined in an expression (instead of lists) which improves readability of the code. Besides that, no additional features are added.

Usage

1

Arguments

expr

an expression

See Also

Private-class

Examples

 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
## Not run: 
  vignette("Introduction", "aoos")

## End(Not run)

# Minimal example:
Test <- defineRefClass({
  Class <- "Test" # this is passed as argument to setRefClass
  x <- "character" # all objects which are not functions are fields
  do <- function() cat("Yes, Yes, I'm working...") # a method
})

test <- Test()
test$x <- "a"
test$do()

# Inheritance and privacy:
pTest <- defineRefClass({
  Class <- "pTest"
  # Privacy is solved by inheriting from a class 'Private' which redefines
  # the methods for access.
  contains <- c("Test", "Private") # passed as argument to setRefClass

  .y <- "numeric" # this is going to be 'private'

  doSomething <- function() {
    .self$.y <- 42
    cat(x, .y, "\n")
    invisible(.self)
  }
})

instance <- pTest()
instance$x <- "Value of .y:"
instance$doSomething()

# A notion of privacy:
stopifnot(inherits(try(instance$.y), "try-error"))
stopifnot(inherits(try(instance$.y <- 2), "try-error"))

aoos documentation built on May 2, 2019, 3:47 p.m.