r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
405 stars 56 forks source link

access private member from other instances of the same class #109

Closed guolinke closed 7 years ago

guolinke commented 7 years ago

example code:

test <- R6Class("Test",
    public = list(
                a = 10,
                fun = function(other){
                    other$c 
                },
                setc = function(c){
                    private$c <- c
                },
                getc = function(){
                    private$c
                }
            ),
    private = list( c = 20 ) 
)

x <- test$new()
y <- test$new()
y$setc(100)
y$getc()
x$fun(y)

for x$fun(y) it should return 100, but it is NULL now.

Is there any way that I can access private member from other instances of the same class?

wch commented 7 years ago

Sorry, there isn't a way to do that. You could access any R6 object's private fields with x$.__enclos_env__$private, but that's a bit of hack.