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

shadowing a regular binding with an active binding #172

Open
romainfrancois opened this issue Nov 27, 2018 · 1 comment
Open

shadowing a regular binding with an active binding #172

romainfrancois opened this issue Nov 27, 2018 · 1 comment
Labels
feature a feature request or enhancement

Comments

@romainfrancois
Copy link

library(R6)

A <- R6Class("A", 
  public = list(
    foo = 3L
  )
)

B <- R6Class("B", inherit = A, 
  active = list(
    foo = function() 91
  )
)

b <- B$new()
#> Error in makeActiveBinding(name, active[[name]], public_bind_env): symbol already has a regular binding

Can we do one of these here:

https://github.com/r-lib/R6/blob/master/R/new.R#L121

  • check that the foo binding already exist in public_bind_env and remove it. (I can submit a PR easily, and I think it's the right approach)

  • give a better error message, explaining what the problem is and what classes and binding are involved. it's easy enough to see what happens in the reprex, but I've been struggling since yesterday with a version of this in 📦 arrow. I guess I can also submit a PR for this option.

@wch
Copy link
Member

wch commented Nov 27, 2018

This looks related to #166 and #168. I think that for subclasses there should be better checks that ensure that public/private/active items are not overridden by items from another category. This would be the second solution you proposed.

Allowing a field to be overridden by an active binding could result in tricky problems. For example, if x is an active binding, in a method you can access super$x. However, if x is a regular (public) field, there is no super$x, only self$x.

library(R6)

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

B <- R6Class("B", inherit = A,
  public = list(
    get_self_x  = function() self$x,
    get_super_x = function() super$x
  ),
  active = list(
    y = function() super$y + 1
  )
)

b <- B$new()
b$get_self_x()
#> [1] 1
b$get_super_x()
#> NULL
b$y
#> [1] 2

@hadley hadley added the feature a feature request or enhancement label Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants