r-lib / R6

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

[bug] active binding detaches on clone #108

Closed epicfarmer closed 7 years ago

epicfarmer commented 7 years ago

In the following code, what should be an active binding is treated as a function. Follow up to #107 . In the last line, I expect test_4$foo to return a value, but instead it returns the function code.

class_1= R6::R6Class(
    private = list(
        bar = 1
    ),
    active = list(
        foo = function(value){
            if(missing(value)){
                return(private$bar)
            }
            private$bar = value
        }
    )
)

class_2= R6::R6Class(inherit = class_1,
    active = list(
        foo = function(value){
            private$bar = private$bar/2
            if(missing(value)) {return(super$foo)}
            super$foo <- value
        }
    )
)

test_1 <- class_1$new()
test_1$foo <- 3
test_1$foo
test_2 <- class_2$new()
test_2$foo <- 3
test_2$foo
test_3 = test_1$clone(TRUE)
test_3$foo
test_4 = test_2$clone(TRUE)
test_4$foo
wch commented 7 years ago

It looks like when the test_2 object is cloned, the clone's super object's active binding is not being made active.

epicfarmer commented 7 years ago

that seems correct