title: How to implement copy constructor in Swift subclass?
body: <p>I have the following example in a Swift playground, in an attempt to implement a copy constructor in Swift:</p>
  
  <pre><code>class Shape : NSObject {
      var color : String
  
      override init() {
          color = "Red"
      }
  
      init(copyFrom: Shape) {
          color = copyFrom.color
      }
  }
  
  class Square : Shape {
      var length : Double
  
      override init() {
          super.init()
          length = 10.0
      }
  
      init(copyFrom: Square) { /* Compilation error here! */
          super.init(copyFrom: copyFrom)
          length = copyFrom.length
      }
  }
  
  let s : Square = Square()      // {{color "Red"} length 10.0}
  
  let copy = Square(copyFrom: s) // {{color "Red"} length 10.0}
  
  s.color = "Blue"               // {{color "Blue"} length 10.0}
  s                              // {{color "Blue"} length 10.0}
  copy                           // {{color "Red"} length 10.0}
  </code></pre>
  
  <p>The problem is that this doesn't actually compile in its current form. On the <code>init(copyFrom: Square)</code>  method in the <code>Square</code> subclass, this error is reported:</p>
  
  <p><code>Overriding method with selector 'initWithCopyFrom:' has incompatible type '(Square) -&gt; Square'</code></p>
  
  <p>This issue <strong>would make sense</strong> if it wasn't a <em>constructor</em>, as if it were a regular <code>func</code>, you could potentially pass in a type that is expected in the superclass, but that has been overridden in the subclass to be more restrictive:</p>
  
  <pre><code>let mySquare : Shape = Square()  // Note the var is a SHAPE
  mySquare.someShapeMethod("Test") // If Square overrides someShapeMethod() to expect Int, compiler errors out to protect us here.
  </code></pre>
  
  <p>But the fact that it's a <em>constructor</em> leads me to believe that I should be able to override it and provide a different method signature, since it's absolutely known at compile time what the type of the object is.</p>
  
  <p>This issue disappears if I alter <code>Shape</code> to no longer extend <code>NSObject</code>. However, due to inclusion with an existing Objective-C code, it needs to extend <code>NSObject</code>.</p>
  
  <p>How can I update my copy constructor to allow a <code>Shape</code> to know it's copying from a <code>Shape</code>, and allow a <code>Square</code> to know it's copying from a <code>Square</code>?</p>