This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1258671-advanced-swift-protocol-oriented-programming/lessons/6
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1258671-advanced-swift-protocol-oriented-programming/lessons/6
@rayfix, could you please elaborate a little bit more the witness-table and dynamic dispatch for value types concepts? Is the witness-table related to the type that conforms to the protocol or the protocol itself? Does w-table only list the methods declared in the protocol and customization points right? not those on the protocol extension which use direct dispatch?
As I understand it (we are getting pretty deep here), every protocol creates a protocol descriptor. When a type conforms to that protocol it creates a conformance record in that descriptor there’s a field that points to a witness table for that type. So there is a unique witness table for every protocol-and-conforming-type combination.
To review, yes, you are correct. If you put a property or function into the formal protocol it is a customization point that can be overridden by a particular type. If you put an implementation in the extension for that protocol, it becomes the default implementation that you can override.
If it is not declared in the formal protocol declaration but just an extension it is statically dispatched and can’t be overridden by a conforming type.
Depending on visibility, the compiler can still devirtualize or make a fast specialization in the case of a generic constraint. I will talk a little more about those in a …coming real soon now… course.