title: instantiate from protocol.Type reference dynamically at runtime
body: <p>I've asked this question earlier at so you get to know a bit of history, It was an awesome attempt by Airspeed Velocity there but I feel I didn't quite get there yet, so I'm narrowing down my question to very minute details in order to really crack it down.</p>
  
  <p><a href="https://stackoverflow.com/questions/27530724/swift-program-to-interface">swift program to interface</a></p>
  
  <p>You may complain or down vote that question is incomplete but that's how it goes, It's based on design patterns, so if you're not familiar with design patterns or philosophy <strong>"Program to interface not to an implementation"</strong> then don't complaint or down vote.</p>
  
  <p>Looking out for SWIFT guru's who can crack it down. </p>
  
  <p>All the best.</p>
  
  <pre><code>public protocol IAnimal {
      init()
      func speak()
  }
  
  class Test {
       func instantiateAndCallSpeak(animal:IAnimal.Type) {
           //use the animal variable to instantiate and call speak - 
           //no implementation classes are known to this method
           //simply instantiate from the IAnimal reference at run time.
           //assume the object coming in via does implement the protocol (compiler checks that)
  
       }
  }
  </code></pre>
  
  <p><strong>Edit
  Awesome Martin</strong>... you cracked it.
  sorry I missed this part, </p>
  
  <p>suppose if you've an array of all those implementation classes, how'd you iterate instantiate and call speak (remember implementation class Cat in this case is not known to the test)</p>
  
  <pre><code>var animals:[IAnimal.Type] = [Cat.self, Dog.self, Cow.self] 
  
  //and so many more implementation classes not known to test method
  </code></pre>
  
  <p>//my attempt in playground causing some issues with it, compiler thrown an error Segmentation fault11</p>
  
  <pre><code>for animal in animals {
      let instance = animal()
      instance.speak()
  }
  </code></pre>