From ClausJonathan.Fritzemeier at uni-duesseldorf.de  Tue Feb 21 12:07:24 2012
From: ClausJonathan.Fritzemeier at uni-duesseldorf.de (Claus Jonathan Fritzemeier)
Date: Tue, 21 Feb 2012 12:07:24 +0100
Subject: [Rd] Private Variables in R5-Classes possible?
Message-ID: <4F437AEC.2060401@uni-duesseldorf.de>

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 at .xData)){
return(get("secret_value", envir=.self at .xData, inherits = F))
}
else{
return(NULL)
}
}
assign("secret_value", value=value, , envir=.self at .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"



[Rd] Private Variables in R5-Classes possible?
Claus Jonathan Fritzemeier