Hi,
I have been playing with alternative ways to assign values to fields in
reference classes.
Besides the "standard" <<- there are two other ways that also "seems to
work". One is using .self$fieldName assigned with <- and the the other
one is field(fieldName, value).
With regards to the ".self method" (example below): is this a "safe"
equivalent to <<- i.e. are these two ways identical in result and
possible "side effects"?
StandardAssignment <- setRefClass(
Class ="StandardAssignment",
fields = list(
m_attributes = "list"
),
methods = list(
setAttribute = function(name, value) {
m_attributes[[as.character(name)]] <<- as.character(value)
return(invisible(.self))
},
getAttribute = function(name) {
m_attributes[[name]]
}
)
)
s1 <- StandardAssignment$new()
s1$setAttribute("name", "foo")
s1$getAttribute("name")
[1] "foo"
SelfAssignment <- setRefClass(
Class ="SelfAssignment",
fields = list(
m_attributes = "list"
),
methods = list(
setAttribute = function(name, value) {
.self$m_attributes[[as.character(name)]] <- as.character(value)
return(invisible(.self))
},
getAttribute = function(name) {
m_attributes[[name]]
}
)
)
s2 <- SelfAssignment$new()
s2$setAttribute("name", "foo")
s2$getAttribute("name")
[1] "foo"
Best regards,
Per
______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel