Member-only story
That little Swift Bug: Protocol Customization Points
It all went wrong!
Difficulty: Beginner | Easy | Normal | Challenging
When you are looking through the use of Protocols in Swift, you’ll come across [Dynamic Dispatch].
When a type adopts a protocol
and an instance of the type is inferred to be a concrete type, all of the protocol methods are statically dispatched at compile time.
So a concrete instance of a Dog
can conform to a protocol Animal
, and here fluffy
is inferred to be of concrete type Dog
. Since this is using Swift's type inference, both the property name
and the method func animalSound() -> String
are statically dispatched at runtime.
The code:
The issue
Let us first take the code. If we accept that tents can be a type of building, and that dome tents and tunnel tents are indeed tents, this will help us to understand the following example: