retList: Generic constructor function

Description Usage Arguments Details See Also Examples

Description

This functions can be used to construct a list with class attribute and merged with another list called super. The constructed list will contain (by default) all visible objects from the environment from which retList is called.

Usage

1
2
3
4
5
6
7
8
retList(class = NULL, public = ls(envir), super = list(),
  superEnv = asEnv(super), mergeFun = envMerge, envir = parent.frame())

funNames(envir = parent.frame())

asEnv(x)

stripSelf(x)

Arguments

class

character giving the class name.

public

character with the names to include.

super

a list/object to be extended.

superEnv

environment where new methods will live in.

mergeFun

function with two arguments. Knows how to join/merge environments - mergeFun(envir, superEnv). Default: envMerge.

envir

this is the environment you want to convert into the list. Default is the environment from which the function is called.

x

a list

Details

funNames returns the names of functions in the environment from which it is called.

asEnv trys to find an environment for x. If x is NULL or an empty list, the function returns NULL. (Else) If x has an attribute called .self it is this attribute which is returned. (Else) If x is a list it is converted to an environment.

See Also

ls, +.Infix, print.Print

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# To get a quick overview of the package:
vignette("Introduction", "aoos")

# To get more infos about retList:
vignette("retListClasses", "aoos")

# To get some infos about performance:
vignette("performance", "aoos")

# A simple class with one method:
Test <- function(.x) {
  getX <- function() .x
  retList("Test")
}

stopifnot(Test(2)$getX() == 2)

# A second example inheriting from Test
Test2 <- function(.y) {
  getX2 <- function() .x * 2
  retList("Test2", super = Test(.y))
}

stopifnot(Test2(2)$getX() == 2)
stopifnot(Test2(2)$getX2() == 4)

### Rational numbers example with infix operators and print method

Rational <- function(numer, denom) {

  gcd <- function(a, b) if(b == 0) a else Recall(b, a %% b)

  g <- gcd(numer, denom)
  numer <- numer / g
  denom <- denom / g

  print <- function(x, ...) cat(paste0(numer, "/", denom, "\n"))

  ".+" <- function(that) {
    Rational(numer = numer * that$denom + that$numer * denom,
             denom = denom * that$denom)
  }

  ".-" <- function(that) {
    if (missing(that)) {
      Rational(-numer, denom)
    } else {
      .self + (-that)
    }
  }

  # Return only what should be visible from this scope:
  retList(c("Rational", "Infix", "Print"),
          c("numer", "denom", "neg", "print"))

}

rational <- Rational(2, 3)
rational + rational
rational - rational

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