Duck typing and polymorphism
Often duck typing is assimilated to polymorphism, since it allows in some way to uniformly manage different type of objects. However, they are different concepts.
Polymorphism is a concept found on types whereas duck typing is found on contracts. With polymorphism, it is important what an object is and not how it behaves. In duck typing, it is important how an object behaves. Duck typing is more tied to the concept of objects that interact, rather than objects that are of a certain type.
However, duck typing can help to implement solutions that strongly typed languages resolve with true polymorphism. Let's consider an example where our software house wants to implement a method that creates a list of full names of all ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access