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

Deep copy of R6 classes with inheritance #72

Closed
kablag opened this issue Aug 17, 2015 · 1 comment

Comments

@kablag
Copy link

commented Aug 17, 2015

Maybe this is very strange class usage, but it does not work after object cloning.

baseClass <-
R6Class("baseClass",
public = list(
initialize = function() {},
ShowVal = function() {
private$.val
}),
private = list(
.val = 1,
deep_clone = function(name, value) {
if (value %>%
class %>%
tail(1) != "R6") {
value
} else {
value$clone(deep = TRUE)
}
}
),
active = list(
Val = function() {
private$.val
}
)
)

subClass <-
R6Class("subClass",
inherit = baseClass,
public = list(
ShowVal = function() {
super$ShowVal() + private$.b$ShowVal()
}
),
private = list(
.b = baseClass$new()
))

sub <- subClass$new()
sub2 <- sub$clone(deep = TRUE)
sub$ShowVal()
sub2$ShowVal()

@wch

This comment has been minimized.

Copy link
Member

commented Aug 17, 2015

Simplified example:

library(R6)
baseClass <- R6Class("baseClass",
  public = list(
    ShowVal = function() private$val
  ),
  private = list(
    val = 1
  )
)

subClass <- R6Class("subClass",
  inherit = baseClass,
  public = list(
    ShowVal = function() super$ShowVal()
  )
)

sub <- subClass$new()
sub2 <- sub$clone()
sub$ShowVal()
# [1] 1
sub2$ShowVal()
# Error in super$ShowVal() : object 'private' not found

It looks like, for cloned objects, the super functions aren't getting private in their enclosing environment:

ls(environment(sub$`.__enclos_env__`$super$ShowVal))
# [1] "private" "self"   

ls(environment(sub2$`.__enclos_env__`$super$ShowVal))
# [1] "self"

@wch wch closed this in 8cc7728 Aug 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.