Closed gaow closed 4 years ago
The reason is that the original design of R6 behaved more like R's reference classes: private and public members could be accessed without private$x
or self$y
-- that is, with just x
or y
. Later on, the portable
option was added, and when set to FALSE
, it still has the same behavior. In these cases, it's that important to make sure there are no name collisions.
When portable=TRUE
(the default), it results in the behavior you're used to, where private$
and self$
are required to access members. But even with portable=TRUE
, overlapping names would cause problems when inheritance is involved. The superclass's private and public methods are combined together in the super
object, so it's necessary to enforce different names to avoid collisions.
Thanks @wch for the clarification.
The superclass's private and public methods are combined together in the super object
Looks like this could be improved to overcome the limitation with portable=TRUE
. Otherwise R6
is really nice -- syntactically nicer than C++ and Python's class in my opinion! I do hope down the road this constraint can be removed to make the syntax more elegant.
Unfortunately, it's not possible to change that behavior without breaking a lot of existing code.
@wch If I set a private member and an active binding with the same variable name I get the error:
I understand that public and active should have unique names because of obvious conflict otherwise. But why private vs public / active? Because they can be uniquely identified as
private$name
andself$name
. Currently my variable names are ugly (have to have.X
for private andX
for active binding for example) due to this constraint. If I start working withX
as a private member then later decide to make it an active binding for access outside the class, then I'll have to change all my existingprivate$X
toprivate$.X
(including those in derived classes!) then define the active binding.Such inelegancy has been the biggest hurdle for me to advocate the use of R6 among my colleagues. I'm wondering if there is a good reason for the constraint, or it can be removed. Thanks!