Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods in cloned object can refer to wrong super object #155

Closed
wch opened this issue Sep 12, 2018 · 0 comments · Fixed by #156
Closed

Methods in cloned object can refer to wrong super object #155

wch opened this issue Sep 12, 2018 · 0 comments · Fixed by #156

Comments

@wch
Copy link
Member

wch commented Sep 12, 2018

If there are three levels of inheritance, C1, C2, and C3, and a method defined in C1 is overridden in C2, but not in C3, then in a C3 object's clone, inside the method, the super object will refer to the wrong level -- it will refer to the C2 methods instead of C1.

In the example below, in the clone, the addx method from C2 gets called twice, when it should only get called once.

C1 <- R6Class("C1",
  public = list(
    x = 1,
    addx = function() {
      message("C1 addx")
      self$x + 1000
    }
  )
)

C2 <- R6Class("C2",
  inherit = C1,
  public = list(
    addx = function() {
      message("C2 addx")
      super$addx() + self$x + 10000
    }
  )
)

C3 <- R6Class("C3",
  inherit = C2
)


a <- C3$new()
b <- a$clone()

a$addx()
#> C2 addx
#> C1 addx
#> [1] 11002

b$addx()
#> C2 addx
#> C2 addx
#> C1 addx
#> [1] 21003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant