title: Dynamic Dispatch and Protocols in Swift
body: <p>Consider this (rather tedious) code :</p>
  
  <pre><code>class SCell : NSObject {}
  class SHeader : NSObject {}
  
  class Cell : SCell {}
  class Header : SHeader {}
  struct Model {}
  
  protocol PA {
      typealias Ce = SCell
      typealias He = SHeader
      func doThis(cell : PA.Ce,header : PA.He)
  }
  
  extension PA {
       func doThis(cell : PA.Ce,header : PA.He) {
          print("A's implementation")
      }
  }
  
  protocol PB:PA {
  
  }
  
  extension PB {
      func doThis(cell : PA.Ce,header : PA.He) {
          print("B's implementation")
      }
  }
  
  class A  : PA {
      func doSomethingElse() {
          self.doThis(cell: Cell(), header: Header())
      }
  }
  
  class B : A,PB {
  
  }
  
  class C : B {}
  
  let c = C()
  c.doSomethingElse()
  </code></pre>
  
  <p>To my surprise, this started printing out </p>
  
  <blockquote>
    <p>"A's implementation"</p>
  </blockquote>
  
  <p>I was expecting it to print "B's implementation" since <code>doThis(cell:header)</code> is overridden as a part of <code>PB</code>s default implementation. This surprisingly did not happen. </p>
  
  <p>What's more interesting is that if I do this:</p>
  
  <pre><code>class B: A,PB {
      override func doSomethingElse() {
          self.doThis(cell: Cell(), header: Header())
      }
  }
  </code></pre>
  
  <p>It started to print out </p>
  
  <blockquote>
    <p>B's implementation</p>
  </blockquote>
  
  <p>Why is this happening? </p>