title: Overriding methods in Swift extensions
body: <p>I tend to only put the necessities (stored properties, initializers) into my class definitions and move everything else into their own <code>extension</code>, kind of like an <code>extension</code> per logical block that I would group with <code>// MARK:</code> as well.</p>
  
  <p>For a UIView subclass for example, I would end up with an extension for layout-related stuff, one for subscribing and handling events and so forth. In these extensions, I inevitably have to override some UIKit methods, e.g. <code>layoutSubviews</code>. I never noticed any issues with this approach -- until today.</p>
  
  <p>Take this class hierarchy for example:</p>
  
  <pre><code>public class C: NSObject {
      public func method() { print("C") }
  }
  
  public class B: C {
  }
  extension B {
      override public func method() { print("B") }
  }
  
  public class A: B {
  }
  extension A {
      override public func method() { print("A") }
  }
  
  (A() as A).method()
  (A() as B).method()
  (A() as C).method()
  </code></pre>
  
  <p>The output is <code>A B C</code>. That makes little sense to me. I read about Protocol Extensions being statically dispatched, but this ain't a protocol. This is a regular class, and I expect method calls to be dynamically dispatched at runtime. Clearly the call on <code>C</code> should at least be dynamically dispatched and produce <code>C</code>?</p>
  
  <p>If I remove the inheritance from <code>NSObject</code> and make <code>C</code> a root class, the compiler complains saying <code>declarations in extensions cannot override yet</code>, which I read about already. But how does having <code>NSObject</code> as a root class change things?</p>
  
  <p>Moving both overrides into their class declaration produces <code>A A A</code> as expected, moving only <code>B</code>'s produces <code>A B B</code>, moving only <code>A</code>'s produces <code>C B C</code>, the last of which makes absolutely no sense to me: not even the one statically typed to <code>A</code> produces the <code>A</code>-output any more!</p>
  
  <p>Adding the <code>dynamic</code> keyword to the definition or an override does seem to give me the desired behavior 'from that point in the class hierarchy downwards'...</p>
  
  <p>Let's change our example to something a little less constructed, what actually made me post this question:</p>
  
  <pre><code>public class B: UIView {
  }
  extension B {
      override public func layoutSubviews() { print("B") }
  }
  
  public class A: B {
  }
  extension A {
      override public func layoutSubviews() { print("A") }
  }
  
  
  (A() as A).layoutSubviews()
  (A() as B).layoutSubviews()
  (A() as UIView).layoutSubviews()
  </code></pre>
  
  <p>We now get <code>A B A</code>. Here I cannot make UIView's layoutSubviews dynamic by any means.</p>
  
  <p>Moving both overrides into their class declaration gets us <code>A A A</code> again, only A's or only B's still gets us <code>A B A</code>. <code>dynamic</code> again solves my problems.</p>
  
  <p>In theory I could add <code>dynamic</code> to all <code>override</code>s I ever do but I feel like I'm doing something else wrong here.</p>
  
  <p>Is it really wrong to use <code>extension</code>s for grouping code like I do?</p>