I have a reference class that I'd to use as an object inside another reference class. Example:
class_1 <- setRefClass( Class = "class_1"
, fields = list(nickname = "character", version = "character" )
, methods = list(
initializer = function(nickname, version) {
nickname <<- nickname
version <<- version
}
)
)
class_2 <- setRefClass( Class = "class_2"
, fields = c( version = "character"
, nickname = "character"
, class_1_item = "class_1" )
, methods = list(
initializer = function(class_2_nickname = "B", class_2_version = "V2") {
class_1_item <<- class_1$new(class_2_nickname, class_1_version)
nickname <<- class_2_nickname
version <<- class_2_version
}
)
)
#######
class_2_obj <- class_2$new(nickname = "A", version = "V1")
class_1_obj <- class_1$new(nickname = "A", version = "V1")
class_2_obj2 <- class_2$new()
When I invoke the first line after the comment markings, it creates a class_2 object with a class_1 object inside of it, but it never initializes the fields to the object class_1_item that has its constructor called as part of the first line. Yet when I call the constructor directly in line 2 (outside of the class 2 constructor), it initializes those fields just fine. Lastly, when I call the constructor in line 3 without arguments, it doesn't even grab the default arguments and leaves everything null.
I feel like there's something fundamental about R classes that makes them completely different from C/Python/Java classes that I'm not getting. I don't understand what the object "class_1" is referring to as an object when I use the assignment operator with $setRefClass()$. Also, I feel like I don't understand in what cases the "<<-" operator is intended to be used in this context versus the "<-" operator.
What am I missing?