Dispatch in Protocols

Because the variable is declared as Greetable, and leave is not a requirement for the protocol.
Maybe it helps to think of the situation this way:
The compiler knows that the object — no matter what type it actually has — has a greet-method; either a custom one, or the default implementation from the protocol as a fallback.

For leave on the other hand, it is only clear that there is an implementation from the protocol, so that is called.

Wouldn't it be possible to call the custom implementation anyways?
Technically, probably yes... but it would be more brittle:
Methods can be declared in many different places (and modules). For example, what should happen if there is a second leave supplied by another protocol the type conforms to? Should that also be preferred over the method from the "right" protocol, which hopefully designed its methods so that they make sense in combination?

1 Like