[Rd] Private Variables in R5-Classes possible?

From: Claus Jonathan Fritzemeier <ClausJonathan.Fritzemeier_at_uni-duesseldorf.de>
Date: Tue, 21 Feb 2012 12:07:24 +0100


Hi list,

is there a way to define some kind of private Variable?

I would like to prevent the user from manipulating fields on his own, in order to not destroy data structures.

The problem is, that as soon as the variable exists in the environment it is accessible via t$secret_value.

test <- setRefClass("test", fields=list( secret = function(value){
cat("the function was called\n")
if(missing(value)){
if(exists("secret_value", envir=.self@.xData)){ return(get("secret_value", envir=.self@.xData, inherits = F)) }
else{
return(NULL)
}
}
assign("secret_value", value=value, , envir=.self@.xData) }
) )

> t <- test$new()
> t$secret

the function was called
NULL

> t$secret_value

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : "secret_value" is not a valid field or method name for reference class “test”
> t$secret <- "Blub"

the function was called
> t$secret_value

[1] "Blub"



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 21 Feb 2012 - 13:30:31 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 22 Feb 2012 - 21:40:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive