r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
407 stars 56 forks source link

Strange singleton issue #68

Closed robertzk closed 9 years ago

robertzk commented 9 years ago

I am having a very bizarre issue. I have a complicated R6 class called director that is being subclassed in another package, where it is called engine (so director is portable in package A and engine inherits from director in package B). I see the following behavior.

> engine_class$new("~/dev/modeling.sy") -> b
> b
Director object monitoring ‘/Users/robertk/dev/modeling.sy’.
> engine_class$new("~/dev/base.sy") -> s
> s
Director object monitoring ‘/Users/robertk/dev/base.sy’.
> b
Director object monitoring ‘/Users/robertk/dev/base.sy’.

Should I look further into my director object definition and strive to produce a minimal reproducible example, or could this be an R6 issue, and do you have any ideas for how to start debugging this?

robertzk commented 9 years ago

My R version is 3.0.2 and my R6 version is 2.1.0.

robertzk commented 9 years ago

I have replicated the issue in the simplest case when

engine_class <- R6::R6Class("engine", portable = TRUE, inherits = director:::director_)
wch commented 9 years ago

Do any of the fields in director_ contain reference objects? Might be related to this: https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html#fields-containing-reference-objects.

robertzk commented 9 years ago

Interesting! I did not know about that behavior, although that was not it. I was actually refactoring director_ from refClass to R6, and it seems I forgot a few self references, so it was setting global variables instead! One-line utility methods that didn't have test coverage.

Thanks for the fast response.

robertzk commented 9 years ago

It makes sense to store the initial default values in the R6 definition, so the reference-type member initializer issue makes sense; reminds me of the care needed around python default arguments.

wch commented 9 years ago

Oh yeah, I've been bitten by the <<- assignment before.