r-lib / R6

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

Fix references to super in cloned objects, and don't treat non-method functions as methods when cloning #156

Closed wch closed 5 years ago

wch commented 5 years ago

Fixes #155, fixes #94, fixes #133. The latter two issues are that when cloning an object with a function that is not a method, the new object would turn it into a method, and alter the environment.

This now works:

A <- R6Class("A",
  public = list(
    f = NULL
  )
)

a <- A$new()
self <- 1
a$f <- function() self
expect_identical(a$f(), 1)

a2 <- a$clone()
expect_identical(a2$f(), 1)

@thomasp85, @cpsievert, I believe you guys asked about this in an issue in another repo, but I can't remember where it was.