title: swift reflections: how to construct a new struct instance based on reflections?
body: <p>My example consists of:</p>
  
  <ul>
  <li>protocol with read-only properties</li>
  <li>struct implementing that protocol </li>
  <li>"+" operator function</li>
  </ul>
  
  <p>I'd like the "+" <strong>operator</strong> to be able to <strong>work for all implementations</strong> of the <strong>protocol</strong> by creating a <strong>new instance</strong> of the specific type that implements that protocol.</p>
  
  <p>As you can see in the source code below, the operator accepts parameters of type "Aable" (the protocol). Using the <strong>generic placeholder T</strong> to construct a new instance fails with the error "'Aable' cannot be constructed because it has no accessible initializers".</p>
  
  <p>Is that even possible? </p>
  
  <p>Could it be achieved using reflections or some sort of introspection? </p>
  
  <pre><code>protocol Aable {
      var name: String { get }
  }
  
  func +(left: Aable, right: Aable) -&gt; Aable {
      let leftType = left.dynamicType
      //error: 'Aable' cannot be constructed because 
      //       it has no accessible initializers
      return leftType(name: left.name + right.name)
  }
  
  struct A: Aable {
      let name: String
  }
  
  let a1 = A(name: "A #1")
  let a2 = A(name: "A #2")
  
  a1 + a2
  </code></pre>