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

R6 class should inherit from environment #91

richierocks opened this issue Aug 17, 2016 · 3 comments


Copy link

commented Aug 17, 2016

Here's the first example from the R6 vignette:

Person <- R6Class("Person",
  public = list(
    name = NA,
    hair = NA,
    initialize = function(name, hair) {
      if (!missing(name)) self$name <- name
      if (!missing(hair)) self$hair <- hair
    set_hair = function(val) {
      self$hair <- val
    greet = function() {
      cat(paste0("Hello, my name is ", self$name, ".\n"))
ann <- Person$new("Ann", "black")

ann is an environment, but its class doesn't inherit from environment.

## [1] TRUE
## [1] "Person" "R6"

This means that S3 methods that work on environments don't work when you call the generic function.

## Error in as.vector(x, "list") : 
##   cannot coerce type 'environment' to vector of type 'list'
## 2: as.list.default(ann)
## 1: as.list(ann)

as.list.environment(ann) works as expected, but these specific methods are often internal to packages and not available.

It would be useful if R6 objects had class c("R6", "environment").


This comment has been minimized.

Copy link

commented Aug 17, 2016

Personally, I am not sure. The environment is the implementation, but if you want an opaque type, then you don't want to inherit from it. What if in the future R6 will have a different implementation? E.g. a couple of environments instead of one? (Which it is, if you consider private members.)

Also, non-portable classes are different, there the environment includes self AFAIR.


This comment has been minimized.

Copy link
Contributor Author

commented Aug 17, 2016

Fair enough. I suppose I'd settle for having an as.list.R6 method that simply called as.list.environment.


This comment has been minimized.

Copy link

commented Aug 18, 2016

I've thought about this before and I've been on the fence whether to add environment as a class. But I think the idea of having as.list.R6 makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.