This simple example in Python 3 demonstrates how any object may be used in any context until it is used in a way that it does not support.
Output:
If it can be assumed that anything that can swim is a duck because ducks can swim, a whale could be considered a duck; however, if it is also assumed that a duck must be capable of flying, the whale will not be considered a duck.
In some statically typed languages such as Boo3 and D,45 class type checking can be specified to occur at runtime rather than at compile time.
Duck typing is similar to, but distinct from, structural typing. Structural typing is a static typing system that determines type compatibility and equivalence by a type's structure, whereas duck typing is dynamic and determines type compatibility by only that part of a type's structure that is accessed during runtime.
The TypeScript,6 Elm7 and Python8 languages support structural typing to varying degrees.
Protocols and interfaces provide a way to explicitly declare that some methods, operators or behaviors must be defined. If a third-party library implements a class that cannot be modified, a client cannot use an instance of it with an interface unknown to that library even if the class satisfies the interface requirements. A common solution to this problem is the adapter pattern. In contrast, with duck typing, the object would be accepted directly without the need for an adapter.
Template (also called generic) functions or methods apply the duck test in a static typing context; this brings all of the advantages and disadvantages of static versus dynamic type checking. Duck typing can also be more flexible in that only the methods actually called at runtime must be implemented, while templates require implementations of all methods that cannot be proven unreachable at compile time.
In languages such as Java, Scala and Objective-C, reflection may be employed to inspect whether objects implement methods or add necessary methods at runtime. For example, Java's MethodHandle API can be used in this manner.9
"Glossary — Python 3.7.1 documentation". docs.python.org. Retrieved 2018-11-08. https://docs.python.org/3/glossary.html#term-duck-typing ↩
"Python Duck Typing - Example". Techie Hours. 2020-06-28. Archived from the original on 2022-03-31. Retrieved 2020-07-26. https://web.archive.org/web/20220331075920/https://techiehours.com/python/python-duck-typing/ ↩
Boo: Duck TypingArchived October 6, 2008, at the Wayback Machine http://boo.codehaus.org/Duck+Typing ↩
"Dynamic classes and duck typing". https://forum.dlang.org/post/hf1e1h$81s$1@digitalmars.com ↩
"Metaprogramming - duck typing in D". https://stackoverflow.com/questions/16578676/duck-typing-in-d ↩
"SE Radio Episode 384: Boris Cherny on TypeScript". se-radio.net. Retrieved 2019-10-25. https://www.se-radio.net/2019/10/episode-384-boris-cherny-on-typescript ↩
Czaplicki, Evan. "Core Language · An Introduction to Elm". Retrieved 30 January 2017. http://guide.elm-lang.org/core_language.html ↩
"PEP 544 – Protocols: Structural subtyping (static duck typing)". https://peps.python.org/pep-0544/ ↩
"StackOverflow: Implement duck typing using java MethodHandles". Retrieved 13 June 2020. https://stackoverflow.com/questions/62321273/implement-duck-typing-using-java-methodhandles ↩