reference classes

To the best of my knowledge using Class instead of setRefClass only changes the appearance, all features are preserved and the outcome is identical to using setRefClass.

library(aoos)

Person <- defineRefClass({
  Class <- "person" # this is the argument 'Class' in setRefClass
  
  personName <- "character" # this is a field of class 'character'
  
  initialize <- function(name) {
    .self$personName <- name
    .self$greet()
  }
  
  greet <- function() {
    cat(paste0("Hello, my name is ", .self$personName, ".\n"))
  }
  
})

ann <- Person("Ann")
## Hello, my name is Ann.
ann
## Reference class object of class "person"
## Field "personName":
## [1] "Ann"
ann$personName
## [1] "Ann"
ann$personName <- "not Ann"
ann$greet()
## Hello, my name is not Ann.

Independently of the usage of Class you can use the class Private to add a notion of privacy to your reference class. This feature will make all fields with a leading dot inaccessible with the standard $ function.

PrivatePerson <- defineRefClass({
  Class <- "PrivatePerson"
  contains <- "Private" # also just passed as argument to setRefClass
  
  .personName <- "character"
  
  initialize <- function(name) {
    .self$.personName <- name
    .self$greet()
  }
  
  greet <- function() {
    cat(paste0("Hello, my name is ", .self$.personName, ".\n"))
  }
  
})

ann <- PrivatePerson("Ann")
## Hello, my name is Ann.
ann
## Reference class object of class "PrivatePerson"
## Field ".personName":
## [1] "Ann"
stopifnot(inherits(try(ann$.personName, silent = TRUE), "try-error"))
ann$greet()
## Hello, my name is Ann.

There is really not much more to this but for illustration consider the class definition using setRefClass:

removeClass("PrivatePerson")
## [1] TRUE
PrivatePerson <- setRefClass(
  Class = "PrivatePerson", 
  fields = list(.personName = "character"),
  contains = "Private",
  methods = list(
    initialize = function(name) {
      .self$.personName <- name
      .self$greet()
    },
    greet = function() {
      cat(paste0("Hello, my name is ", .self$.personName, ".\n"))
    }
  )
)

ann <- PrivatePerson("Ann")
## Hello, my name is Ann.
ann
## Reference class object of class "PrivatePerson"
## Field ".personName":
## [1] "Ann"
stopifnot(inherits(try(ann$.personName, silent = TRUE), "try-error"))
ann$greet()
## Hello, my name is Ann.