title: How can I make a class property array be a heterogeneous array of a generic type?
body: <p>Here's what I want. I'm writing a very simple <a href="https://gist.github.com/Evertt/339d0eda49018c2f1b8b" rel="nofollow">event dispatcher</a> (click that link to see my code). It was working fine when I only had the <code>listen()</code> and the <code>fire()</code> method. This is how you could use it:</p>
  
  <pre><code>struct UserHasBirthday: Event {
      let name: String
  
      init(name: String) {
          self.name = name
      }
  }
  
  let events = TestDispatcher()
  events.listen {
      (event: UserHasBirthday) in
      print("Happy birthday \(event.name)!")
  }
  events.fire( UserHasBirthday(name: "John Doe") )
  </code></pre>
  
  <p>That's all well and good, but now I wanted to add the feature that you could push events to a queue and then later fire them all at once. That's why I added the push and flush methods.</p>
  
  <p>Now the problem is that in the <code>flush()</code> method I need to be able to downcast the generic <code>Event</code> type to the specific event type that was given. Otherwise the <code>fire()</code> method doesn't work.</p>
  
  <p>So I thought, maybe I could save the type information in the same array as the event itself. As you can see I tried to do that with a tuple. Unfortunately it doesn't work like that.</p>
  
  <p>I think that if I could find a way to make the variable <code>pushedEvents</code> accept a generic type like so: <code>var pushedEvents = Array&lt;E: Event&gt;()</code> then it could work. But the only way I know to do that is to assign that generic to the whole class like so: <code>class TestDispatcher&lt;E: Event&gt; { }</code>, but then every instance of that class can only be used for one specific type of event and I definitely don't want that.</p>
  
  <p>Does anybody know some kind of way to make this work?</p>