title: Environment assigns itself to the same R6 active binding that returned it. Why?
body: <p>I was working on creating an easy to understand inheritance structure for R6 classes and attempted to let sub classes be returned by active bindings to skip creating subclasses using seperate R6 methods. I created 1. a list of subclasses (<code>private$.subclasses</code>), 2. a method to intialize subclasses (<code>private$.get_subclass()</code>) and 3. an active binding that returns the respective list element if it exists and initializes the subclass if it does not.</p>
  <p>This worked like a charm until I tried to implement active bindings in the subclasses. For some reason, after I assign a value to an active binding of a subclass, the active binding that handles the subclass gets called and assigns the class environment (that it returns) to itself. This resulted in a bunch of hair-raising errors. I managed to create a reproducible example, but the question of why it happens remains unanswered:</p>
  <pre><code>TopClass &lt;- R6::R6Class(
    active = list(
      subclass = function(arg) {
  
        # If anything is passed, print it
        if(!missing(arg)) print(arg)
  
        # If the subclass does not exist yet, initialize it
        if (is.null(private$.subclasses$SubClass)) {
          private$.get_subclass(SubClass)
        }
        return(private$.subclasses$SubClass)
      }
    ),
  
    public = list(
      initialize = function() {
        self$subclass
      }
    ),
  
    private = list(
      .subclasses = list(),
  
      .get_subclass = function(env) {
        env_name &lt;- deparse(substitute(env))
        private$.subclasses[[env_name]] &lt;- env$new()
      }
    )
  )
  
  
  # This class is initialized using an active binding
  
  SubClass &lt;- R6::R6Class(
    active = list(
  
      # As soon as I assign a value to this active binding, the SubClass is passed to TopClass$subclass
      # This does NOT happen if I do not pass an argument to the binding!
      assign_x = function(x) {
        if (!missing(x)) {
          private$.field_storage &lt;- x
          print(private$.field_storage)
        }
        return(private$.field_storage)
      }
    ),
  
    private = list(
      .field_storage = NULL
    )
  )
  </code></pre>
  <p>If I initialize the TopClass and try to assign a value to <code>$subclass$assign_x</code>, the active binding <code>$subclass</code> gets called and is passed the environment <code>SubClass</code>.</p>
  <pre><code>&gt; t &lt;- TopClass$new()
  &gt; t$subclass$assign_x &lt;- 5
  
  [1] 5  # Should be here!
  &lt;R6&gt;   # Should not be here!
    Public:
      assign_x: active binding
      clone: function (deep = FALSE) 
    Private:
      .field_storage: 5
  </code></pre>
  <p>This does not happen if I do not assign a value to <code>assign_x</code>:</p>
  <pre><code>&gt; t$subclass$assign_x
  [1] 5
  </code></pre>
  <p>While there's surely workarounds for this, I'd like to know why exactly this happens because I cannot get my head around it.</p>