ReferenceClasses and object composition in R

115 Views Asked by At

I want to do basic object composition in R and I'm facing this simple problem. I have 2 R5 classes, "Lambda" and "Composition".
The class "Composition" has an attribute of class "Lambda". Class "Composition" can't be created:".Object$initialize(...) : argument "lambda" is missing, with no default" It works if lambda has a default value in the initialize method : initialize = function(lambda=1){ but I don't want that.

setRefClass(
    "Lambda",
     fields =  c(
       lambda = "numeric"
        ),

      methods=list(
        initialize = function(lambda){
        check_lambda (lambda)
         lambda <<- lambda
        },

       check_lambda = function(new_lambda){
          print ("checking...")
          return(T)
        }
      )
 )

setRefClass(
  "Composition",
    fields =  c(
      object_lambda = "Lambda"
    ),
  methods=list(
    initialize = function(object_lambda){
      object_lambda <<- object_lambda
    }
  )
)

Thanks for your help.

1

There are 1 best solutions below

0
On

I found a solution to my question : set the field to "ANY" of my class "Composition" :

 setRefClass(
  "Composition",
  fields =  c(
    object_lambda = "ANY"
  ),
  methods=list(
    initialize = function(object_lambda){
      object_lambda <<- object_lambda
    }
  )
)