write the code for an S4 class with setters and getters

clgen(
  clname = "Turtle",
  protolist = list(location = c(0, 0), orientation = 0, path = matrix(c(0, 0), ncol =
    2)),
  check_init = FALSE
)

Arguments

clname

character(1) class name

protolist

named list() of prototypical values for slots

check_init

logical(1) if TRUE, try to construct instance with prototype before building code

Note

The example is inspired by Stuart Lee's blog post

Examples

dm = clgen() dm
#> ClassSupport instance for Turtle #> slots: location orientation path
dumpcs(dm)
#> setClass(‘Turtle’, slots= c(location=‘numeric’, orientation=‘numeric’, path=‘matrix’), prototype=list(location = c(0, 0), orientation = 0, path = structure(c(0, 0), .Dim = 1:2))) #> setGeneric(name=‘location’, def=function(x) standardGeneric(‘location’)) #> setMethod(f=‘location’, signature=‘Turtle’, function(x) slot(x, ‘location’)) #> setGeneric(name=‘orientation’, def=function(x) standardGeneric(‘orientation’)) #> setMethod(f=‘orientation’, signature=‘Turtle’, function(x) slot(x, ‘orientation’)) #> setGeneric(name=‘path’, def=function(x) standardGeneric(‘path’)) #> setMethod(f=‘path’, signature=‘Turtle’, function(x) slot(x, ‘path’)) #> setGeneric(name=‘location<-’, def=function(x, value) standardGeneric(‘location<-’)) #> setMethod(f=‘location<-’, signature=‘Turtle’, function(x, value) {slot(x, ‘location’) = value; x}) #> setGeneric(name=‘orientation<-’, def=function(x, value) standardGeneric(‘orientation<-’)) #> setMethod(f=‘orientation<-’, signature=‘Turtle’, function(x, value) {slot(x, ‘orientation’) = value; x}) #> setGeneric(name=‘path<-’, def=function(x, value) standardGeneric(‘path<-’)) #> setMethod(f=‘path<-’, signature=‘Turtle’, function(x, value) {slot(x, ‘path’) = value; x})