Hello, testclass <- setRefClass( "testclass", fields = list(testfield = "logical"), methods = list(validate=function(){testfield<<-TRUE}))> test <- testclass$new() > test$testfieldlogical(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
On Oct 24, 2012, at 2:14 AM, Johannes Graumann wrote:> 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? >The fact that you used only "lock" in your code and I am unable to find such a function makes me wonder whether that was an implicit psuedo-code effort and that you do not know about: ?lockBinding -- David Winsemius, MD Alameda, CA, USA
David Winsemius wrote:> > On Oct 24, 2012, at 2:14 AM, Johannes Graumann wrote: > >> 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? >> > > The fact that you used only "lock" in your code and I am unable to > find such a function makes me wonder whether that was an implicit > psuedo-code effort and that you do not know about: > > ?lockBindinglockbinding does in fact what I want, but your statement about pseudo-code is not entirely correct ... try the following ("?setRefClass" will light the way).> testclass <- setRefClass("testclass", fields = list(testfield = "logical"))> testclass$lock("testfield") > test <- testclass$new() > test$testfield <- TRUE > test$testfield <- FALSEI was assuming to be able to have access to the "lock" method from a class instance as well (rather than just in the class definition) ... Is that indeed impossible? Cheers, Joh