2

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",
  ...
)
pan0ramic
  • 183
  • 9
  • I think I solved the issue: Create a generator, lock the fields, and export the generator. This seems to have worked. So in my case I exported "generator": `generator <- getRefClass(ref_class_name)` after `generator$lock(read_only_field)` – pan0ramic Mar 26 '16 at 00:51

0 Answers0