[R] R5: Lock a class field from within a method?

Johannes Graumann johannes_graumann at web.de
Wed Oct 24 11:14:02 CEST 2012


Hello,

testclass <- setRefClass(
  "testclass",
  fields = list(testfield = "logical"),
  methods = list(validate=function(){testfield<<-TRUE}))

> test <- testclass$new()
> test$testfield
logical(0)
> test$validate()
> test$testfield
[1] TRUE

Works just fine for me.

I would love to be able to do something like

testclass <- setRefClass(
  "testclass",
  fields = list(testfield = "logical"),
  methods = list(validate=function(){
	testfield<<-TRUE
	.self$lock(testfield)
  }))

but am unabel to achieve that. Can anyone point out how to go about 
rendering a field immutable after execution of a specific method?

Sincerely, Joh



More information about the R-help mailing list