Protocol extension static method generic selection question

This is a little bit tricky. In generic function we use protocol only as generic constraint, and we might expect that after generic specialization pass our generic type will be be transformed to a concrete Type. And, if so, the greeting function of that concrete type will be called. But it is not.
Firstly, concrete type can not be always specialized. In this post it is explained: SE-0309: Unlock existential types for all protocols - #158 by Joe_Groff
Secondly, all we know in generic context about a Type is information from generic constraints. Some concrete types might have the same methods as in protocol extension. But others don't. So in generic context we can only rely on what is available in protocol, not in concrete type.

func greet(_ AnimalType: Animal.Type) - this is not a generic function, but it will print the same.

The main point here is that you need to add this method in protocol requirements, not in extension. Then even in generic context or in protocol type it can be proven, that this method exists in all concrete types and can be found in witness table.

1 Like