[swift-evolution] [Discussion] Allowing subclasses to override requirements satisfied by defaults

Adrian Zubarev adrian.zubarev at devandartist.com
Fri May 20 11:47:20 CDT 2016


Do we still get this in Swift 3?

https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#allowing-subclasses-to-override-requirements-satisfied-by-defaults-

protocol P {}

extension P {
  func foo() { print("P") }
}

class C : P {
  // gets the protocol extension's  
}

class D : C {
  /*override not allowed!*/ func foo() { print("D") }
}

let p: P = D()
p.foo() // gotcha: prints "P" rather than "D"!

What will happen if I declare a final computed property or function inside the default implementation? 

protocol P {}

extension P {
  final func foo() { print("P") }
}

class C : P {
  // gets the protocol extension's  
}

class D : C {
  /* can not override because of final */ override func foo() { print("D") }
}

let p: P = D()
p.foo() // prints "P"

-- 
Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160520/fb2cdf9b/attachment.html>


More information about the swift-evolution mailing list