r-lib / R6

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

Autocomplete for R6 classes #53

Closed wahani closed 9 years ago

wahani commented 9 years ago

I was wondering why there seems to be no autocomplete (in R and RStudio) for R6 classes since they are environments and for 'regular' environments autocomplete is supported. I played around with the objects and noted that adding "environment" to the list of classes enables autocomplete again.

Queue <- R6Class("Queue",
                 public = list(
                   initialize = function(...) {
                     for (item in list(...)) {
                       self$add(item)
                     }
                   },
                   add = function(x) {
                     private$queue <- c(private$queue, list(x))
                     invisible(self)
                   },
                   remove = function() {
                     if (private$length() == 0) return(NULL)
                     # Can use private$queue for explicit access
                     head <- private$queue[[1]]
                     private$queue <- private$queue[-1]
                     head
                   }
                 ),
                 private = list(
                   queue = list(),
                   length = function() base::length(private$queue)
                 )
)

tmp <- Queue$new()

class(tmp) <- c("Queue", "R6", "environment")
class(Queue) <- c("R6ClassGenerator", "environment")

I don't see if there is a particular reason to not adding it to the list of classes. I am aware of the argument class = FALSE but maybe the above would be a better default?

wch commented 9 years ago

Tab completion seems to work now. See https://github.com/wch/R6/issues/43#issuecomment-114224019.