In computer programming, duck typing is an application of the duck test in type safety. It requires that type checking is deferred to runtime, and is implemented by means of dynamic typing or reflection.
Duck typing is concerned with establishing the suitability of an object for some purpose. With normal typing, suitability is assumed to be determined by an object's type only. In duck typing, an object's suitability is determined by the presence of certain methods and properties (with appropriate meaning), rather than the actual type of the object. The analogy to the duck test appeared in a Python Enhancement Proposal discussion in 2002.
- 1 Overview
- 2 Concept examples
- 3 In statically typed languages
- 4 Comparison with other type systems
- 5 Criticism
- 6 Criticism of the term itself
- 7 History
- 8 Language support
- 9 See also
- 10 References
- 11 External links
In duck typing, a programmer is only concerned with ensuring that objects behave as demanded of them in a given context, rather than ensuring that they are of a specific class. For example, in a non-duck-typed language, one would create a function that requires that the object passed into it be of type Duck, or descended from type Duck, in order to ensure that that function can then use the object's walk and quack methods. In a duck-typed language, the function would take an object of any type and simply call its walk and quack methods, producing a run-time error if they are not defined. Instead of specifying types formally, duck typing practices rely on documentation, clear code, and testing to ensure correct use.
Consider the following pseudo-code for a duck-typed language:
function calculate(a, b, c) => return (a + b)*c example1 = calculate (1, 2, 3) example2 = calculate (, [2, 3], 2) example3 = calculate ('apples ', 'and oranges, ', 3) print to_string example1 print to_string example2 print to_string example3
In the example, each time the
calculate function is called, objects without related inheritance may be used (numbers, lists and strings). As long as the objects support the "+" and "*" methods, the operation will succeed. If translated to Ruby or Python, for example, the result of the code would be:
9 [1, 2, 3, 1, 2, 3] apples and oranges, apples and oranges, apples and oranges,
The duck test can be seen in the following example (in Python). As far as the function
in_the_forest is concerned, the
Person object is a duck:
class Duck: def quack(self): print("Quaaaaaack!") def feathers(self): print("The duck has white and gray feathers.") class Person: def quack(self): print("The person imitates a duck.") def feathers(self): print("The person takes a feather from the ground and shows it.") def name(self): print("John Smith") def in_the_forest(duck): duck.quack() duck.feathers() def game(): donald = Duck() john = Person() in_the_forest(donald) in_the_forest(john) game()
In statically typed languages
Certain usually statically typed languages such as Boo and the version 4 release of C# have extra type annotations that instruct the compiler to arrange for type checking of classes to occur at run-time rather than compile time, and include run-time type checking code in the compiled output.
Comparison with other type systems
Structural type systems
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 run time.
Protocols and Interfaces
Protocols and interfaces can provide some of the benefits of duck typing, but duck typing is distinct in that no explicit interface is defined. For example, if a third party Java library implements a class you are not allowed to modify, you cannot use an instance of the class in place of an interface you have defined yourself, whereas duck typing would allow this. Again, all of an interface must be satisfied for compatibility.
Templates or generic types
Template, or generic functions or methods apply the duck test in a static typing context; this brings all the advantages and disadvantages of static versus dynamic type checking in general. Duck typing can also be more flexible in that only the methods actually called at run time need to be implemented, while templates require implementation of all methods that cannot be proven unreachable at compile time.
One issue with duck typing is that it forces programmers to have a much wider understanding of the code they are working with at any given time. For instance, in Python, one could easily create a class called Wine, which expects a class implementing the "press" attribute as an ingredient. However, a class called Trousers might also implement the press() method. With duck typing, in order to prevent strange, hard-to-detect errors, the developer needs to be aware of each potential use of the method "press", even when it's conceptually unrelated to what they are working on. By way of contrast, in a strongly and statically typed language that uses type hierarchies and parameter type checking, it's much harder to supply an unexpected object type to a class. For example, in a language like Java, the ambiguity in the above reuse of the method name press() would not be a problem unless one of the two classes was deliberately defined as a child of the other.
Criticism of the term itself
Use of the term "Duck typing" has been considered superfluous in light of the fact that other terms, such as dynamic binding, express the concept more clearly. To proponents of Static type checking, Duck typing suggests the absence of typing, making its incorporation of the term typing appear incoherent.
Use of the term "Duck" needlessly introduces indirection that cannot be resolved by understanding the dictionary definition of "Duck". Even after learning the analogy, it is not obvious (even to an otherwise well-versed computer scientist) at which point in the program type checking is actually performed.
In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with.
- "PEP 285: Adding a bool type".
- Boo: Duck Typing
- Anders Hejlsberg Introduces C# 4.0 at PDC 2008
- Bruce Eckel. "Strong Typing vs. Strong Testing". mindview.
- Bill Venners. "Contracts in Python. A Conversation with Guido van Rossum, Part IV". Artima.
- Lippert, Eric (2 Jan 2014). "What is “duck typing”?". Fabulous adventures in coding. Retrieved 25 May 2016.
... the whole idea of duck typing is fundamentally incoherent ...'
- Apache Software Foundation "commons" proxy project provides DuckTyping implementation in Java
- Dr. Dobbs June 01 2005: "Templates and Duck Typing"
- Duck Typing: Ruby
- How to duck type? - the psychology of static typing in Ruby
- Python documentation glossary entry on duck-typing
- Python from a Java perspective - Part 2 - How duck typing influences class design and design principles