[swift-users] Question with calling a method which returns `Self` on super

Joanna Carter joanna at carterconsulting.org.uk
Fri Aug 11 06:30:23 CDT 2017


I really must do more research before making accusations ;-)

> Which still leaves my question of why I have to reimplement the default copy(other:) method from the protocol's extension in the base class ???

Apparently, the answer is to remove the declaration of the copy(other:) method from the protocol itself and to leave the default implementation in the extension !

So I now end up with :

protocol Copyable
{
  init(other: Self)
}

extension Copyable
{
  func copy() -> Self
  {
    return type(of: self).init(other: self)
  }
}


class Shape : Copyable
{
  var color: NSColor
  
  init(color: NSColor)
  {
    self.color = color
  }
  
  required init(other: Shape)
  {
    color = other.color
  }
}

… and all is well with the world :-)

Mind you, the compiler error messages were hardly helpful to diagnosing this particular problem :-(

Joanna

--
Joanna Carter
Carter Consulting



More information about the swift-users mailing list