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
Closed

Deep copy of R6 classes with inheritance #72

kablag opened this issue Aug 17, 2015 · 1 comment

Comments

@kablag
Copy link

@kablag kablag 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
Copy link
Member

@wch wch 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"

Loading

@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
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants