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

object 'super' not found (when using active members and clones) #119

Closed
GregorDeCillia opened this issue Jun 8, 2017 · 3 comments
Closed

object 'super' not found (when using active members and clones) #119

GregorDeCillia opened this issue Jun 8, 2017 · 3 comments

Comments

@GregorDeCillia
Copy link

@GregorDeCillia GregorDeCillia commented Jun 8, 2017

I currently have an issue with cloned objects. My setup looks roughtly like this

library( "R6" )

A = R6Class(
  'A',
  public = list(
    A_method = function(){}
  ),
  active = list(
    ns = function(){}
  )
)

B = R6Class(
  'B',
  inherit = A,
  public = list(
    B_method = function(){
      super$A_method()                  # 18
    }
  )
)

C = R6Class(
  'C',
  inherit = B,
  public = list(
    C_method = function(){
      super$B_method()                  # 28
    }
  )
)

C$new()$C_method()                      # NULL
B$new()$clone()$B_method()              # NULL
C$new()$clone()$C_method()              # Error in super$B_method() : object 'super' not found

It seems like B can't access the methods of its parent A via a call to super (in line # 18). The error does not occur, if the active member ns is ommited

Any help would be appreciated!

@GregorDeCillia GregorDeCillia changed the title object 'super' not found when using active members object 'super' not found (when using active members and clones) Jun 8, 2017
@wch
Copy link
Member

@wch wch commented Jun 8, 2017

This sounds like a duplicate of #108.

@wch
Copy link
Member

@wch wch commented Jun 8, 2017

It turns out this is actually different from #108. The problem doesn't have to do with active bindings per se, but it happens to come up in classes with active bindings, because of the way that as.list.environment sorts active bindings separately from other objects.

Here's a minimal example:

library(R6)

A <- R6Class("A",
  public = list(
    methodA = function() "A"
  ),
  active = list(
    x = function() "x"
  )
)

B <- R6Class("B",
  inherit = A,
  public = list(
    methodB = function() {
      super$methodA()
    }
  )
)

C <- R6Class("C",
  inherit = B,
  public = list(
    methodC = function() {
      super$methodB()
    }
  )
)

C1 <- C$new()
C2 <- C1$clone()
C2$methodC()
# Error in super$methodB() : object 'super' not found

The problem is in this line of code: https://github.com/wch/R6/blob/41f942af/R/clone.R#L58

It makes the assumption that all of the functions in the super object have the same enclosing environment. However, this is not true when there are two levels of inheritance. C's super object may have methods declared in B as well as some that were inherited from A. This is shown by:

lapply(C1$.__enclos_env__$super, environment)
# $x
# <environment: 0x10da95228>
# 
# $clone
# <environment: 0x10da8fce8>
# 
# $methodB
# <environment: 0x10da8fce8>
# 
# $A_method
# <environment: 0x10da95228>
@wch wch closed this in 073b6f0 Jun 8, 2017
@GregorDeCillia
Copy link
Author

@GregorDeCillia GregorDeCillia commented Jun 8, 2017

Thank you for the quick fix. 073b6f0 solved all my issues in the minimal example as well as my actual project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.