I'm trying to make a factory method for creating my reference class objects. I have one field which MUST be read-only, and so I want to give my package users a way to generate these reference class objects with a guaranteed lock on this field.
My RCs and my factory are both in a package. All of this works fine when I'm working outside of the package but I get the following error when I use the RCs in the package:
Error in methods:::.lockRefFields(def, ...) : the definition of class "(my ref class name)" in package 'my package name' is locked so fields may not be modified Calls: test_check ... eval -> build.rc -> ->
It looks like the error comes from this bit in the package:methods source code
if (.classDefIsLocked(def))
stop(gettextf("the definition of class %s in package %s is locked so fields may not be modified",
Important: When loading my source code, I can lock as normal without problems. The problem lies with my RCs being sourced from a package. It appears that their "classDefIsLocked" (whatever that means).
Here is my code that produces the error:
build.rc <- function(read_only_field, read_only_value, ref_class_name) {
generator <- getRefClass(ref_class_name)
# don't relock if the read_only_field has already been locked
if (!(read_only_field %in% generator$lock())) {
generator$lock(read_only_field)
}
return(generator$new(read_only_value))
}
And here is how I define my RC within my package:
#' @exportClass my.ref.class
setRefClass(
Class = "my.ref.class",
...
)