title: In Swift, can Types (not instances) conform to protocols?
body: <p>I have a ViewController that uses a class (call it <code>A</code>) from a framework outside of my control. <code>A</code> provides a class function I need (call it <code>f</code>). I want to make <code>A</code> easy to mock out in tests. </p>
  
  <p>So my idea was to create a protocol <code>P</code> that has the same signature as <code>A</code>, extend A to implement P, and then create a mock class <code>M</code> that also implements <code>P</code> and has a dummy implementation of <code>f</code>. Then in my tests, I could just do <code>viewController.dependency = M</code> and everything should be dandy.</p>
  
  <p>This is easier said than done, for reasons I'm hoping you'll help me understand.</p>
  
  <p>Inside my viewController, it's easy to declare a variable that contains a protocol-conforming instance and then reassign the variable to another Protocol-conforming instance:</p>
  
  <pre><code>// works
  var dependency: P = A()
  dependency.f()
  dependency = M()
  dependency.f()
  </code></pre>
  
  <p>But it doesn't work to do the same thing with just the types:</p>
  
  <pre><code>// doesn't work
  var dependency = A.self
  dependency = M.self  // cannot assign value of M.Type to a value of type A.Type
  
  // also doesn't work
  var dependency: P = A.self // type A.Type does not conform to protocol P
  </code></pre>
  
  <p>Is there a way to make this work? I thought maybe I could use a generic type for <code>dependency</code> but I can't figure out a syntax to declare a generic type for a variable assignment.</p>