title: R - 'unlist' methods for list of custom classes
body: <p>I have some custom classes, for example:</p>
  
  <pre><code>setClass("foo", slots = c(mat = "matrix"))
  </code></pre>
  
  <p>I want to handle how a list of <code>foo</code> objects are 'unlisted'.</p>
  
  <pre><code>mat &lt;- matrix(rnorm(16), 4)
  foo &lt;- new("foo", mat = mat)
  unlist(list(foo))
  </code></pre>
  
  <p>I thought perhaps creating methods for <code>c</code> (which I thought was used but perhaps incorrectly) and <code>unlist</code> would solve the problem.</p>
  
  <p>S3 version</p>
  
  <pre><code>#' @export
  unlist &lt;- function(x, ...) UseMethod("unlist", x)
  
  #' @export
  unlist.default &lt;- base::unlist
  #' @method unlist foo
  #' @export
  unlist.foo &lt;- function(x, ...){
    print("called foo unlist")
  }
  </code></pre>
  
  <p>S4 version</p>
  
  <pre><code>#' @export
  setMethod("unlist",
            signature = "foo",
            function(x, recursive = TRUE, use.names = TRUE){
              print("call foo unlist")
            })
  </code></pre>
  
  <p><code>c</code> function</p>
  
  <pre><code>#' @export
  setMethod("c", 
            signature = "foo",
            function(x, ..., recursive = FALSE){
              print("called foo c")
  })
  </code></pre>
  
  <p>But I only see the confirmation message when I use <code>c</code> directly:</p>
  
  <pre><code>c(foo)
  [1] "called foo c"
  </code></pre>
  
  <p>The <code>unlist</code> just returns the same object with no print message</p>
  
  <pre><code>unlist(list(foo))
  [[1]]
  An object of class "foo"
  Slot "mat":
             [,1]       [,2]       [,3]        [,4]
  [1,]  0.6711541 -0.2783441 -0.4707375 -0.23060105
  [2,]  0.7408401  0.4076826  2.2757187 -0.48547413
  [3,]  1.8640581  0.3610619 -0.4632473 -0.06498348
  [4,] -0.5595930  0.6679157 -0.8142456  0.27499963
  </code></pre>
  
  <p>If I call <code>unlist(foo)</code> then I get the print message but I need to apply it on a list of the <code>foo</code> objects.  Any thoughts on how I can have unlist deal with custom classes within a list?</p>
  
  <p>Ultimately I want the following to return <code>TRUE</code>:</p>
  
  <pre><code>all.equal(unlist(list(foo)), unlist(list(mat)))
  </code></pre>