title: Vectorize an S4 class in R
body: <p>I got some troubles defining array like classes in a way that they are fully typed (as far as that is possible in R).
  My example: I want to define a class <code>Avector</code>, which should contain an arbitrary number of elements of the class <code>A</code>.</p>
  
  <pre><code># Define the base class
  setClass("A", representation(x = "numeric"))
  
  # Some magic needed ????
  setClass("Avector", ???)
  
  # In the end one should be able to use it as follows:
  a1 &lt;- new("A", x = 1)
  a2 &lt;- new("A", x = 2)
  X &lt;- new("Avector", c(a1, a2))
  </code></pre>
  
  <p>I am aware that having a vector of objects is not possible in R. So I guess it will be stored in a kind of "typed" list.
  I have found some solution, but I am not happy with it:</p>
  
  <pre><code># Define the vectorized class
  setClass(
    "Avector", 
    representation(X = "list"), 
    valididty = function(.Object)) {
      if (all(sapply(.Object@X, function(x) class(x) == "A")))
        TRUE
      else
        "'Avector' must be a list of elements in the class 'A'"
    }
  )
  # Define a method to subscript the elements inside of the vector
  setMethod(
    "[", signature(x = "Avector", i = "ANY", j = "ANY"),
    function(x, i, j, ...) x@X[[i]]
  )
  # Test the class
  a1 &lt;- new("A", x = 1)
  a2 &lt;- new("A", x = 2)
  avec &lt;- new("Avector", X = list(a1, a2))
  # Retrieve the element in index i
  avec[i]
  </code></pre>
  
  <p>This method appears more like a hack to me. Is there a way to do this in a canonical way in R without doing this type checking and indexing method by hand?</p>
  
  <p><strong>Edit</strong>:
  This should also hold, if the class <code>A</code> is not consisting of atomic slots. For example in the case that:</p>
  
  <pre><code>setClass("A", representation(x = "data.frame"))
  </code></pre>
  
  <p>I would be glad for help :)
  Cheers,
  Adrian</p>