Duck typing
![]() | This article needs attention from an expert on the subject. Please add a reason or a talk parameter to this template to explain the issue with the article. |
This article may need to be rewritten to comply with Wikipedia's quality standards. (july 2007) |
Duck typing is a principle of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class.
The term is a reference to the duck test —
- When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.
In this context though, it's more correct to say, "I can treat that bird as being like a duck, in at least some aspect".
Unfortunately the term has been popularised by its cute formation from a well-known quotation, rather than through its accuracy or ability to explain the concept. It's misleading in that it's usually applied to languages that actually contradict this "It's a duck" notion.
The scope of dynamic typing is just that of the method call(s), not the entire use of the object. For our purposes we don't care if the object supports both quacking and walking, just what's required at the time. It's fundamental that an inability to swim should not rule out use of the quack behaviour, even if it means that the object couldn't thus be "a true and complete duck".
Alex Martelli made an early (2000) use of the term in a message to the comp.lang.python newsgroup. He also highlighted this misunderstanding of the literal duck test, which may indicate that the term was already in use. [citation needed]
- 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.
Languages (e.g. Java) may define interfaces which are then used in an inheritance-like manner. These are not usually considered to be duck typed languages. However if they implement a duck interface that defines the set of expected behaviours as a single unit, then they are actually closer to the literal "duck test" view than languages considered to be duck typed (e.g. Python) that test compatibility on a method-by-method basis, in a more truly dynamic manner.
Biological classification analogy
By analogy with scientific classification in biology, duck typing is a morphogenetic classification scheme. Object-oriented languages that use classical inheritance tend to take a phylogenetic approach to structuring software systems; that is, they classify objects in terms of their inheritance, or "is-a" relationships, and only objects of the same lineage can be substituted polymorphically.
Some languages, however, including those that use duck typing, allow morphogenetic classification; that is, they allow classification of objects in terms of their "shape" or how they "look". In software object terms, an object's "shape" is determined by its declared interface(s) (for example, in Java) or by the operations it supports (in duck typed languages). In duck typing, the actual inheritance of an object (or phylogeny) is of less importance than the way it "looks" (or morphology) to users of the object.
Dynamic typing across languages
Dynamic typing allows an object to be interchangeable with any other object so long as they both implement sufficiently useful interfaces, regardless of whether the objects have a related inheritance hierarchy. Duck typing is a feature of programming languages such as Smalltalk, Python, Ruby, JavaScript, and ColdFusion.
In Smalltalk, any object is interchangeable with any other at runtime. This is the most flexible kind of dynamic typing. Duck typing attempts to limit this flexibility while eliminating a source of possible errors before runtime. The Smalltalk architects sought to achieve true polymorphism at the expense of possible errors creeping in at runtime. In practice, these errors are dealt with, at runtime, via SUnit testing.
Abstract data types are static interfaces that exist only to guarantee, on paper, a particular interface. Smalltalk uses pure dynamic mechanisms, in a variety of ways, to extend the guarantee in practice. This can be as simple as generalizing the "method not found" exception handler into a catch-all lookup mechanism. But it can involve extending the language or environment, as in StrongTalk. Parallels to Smalltalk's exception handling came to be called duck typing in Java and Python, and a single reasonable syntax emerged.
C++ templates implement a static form of duck typing. An iterator, for example, does not inherit its methods from an Iterator base class.
Yet another approach similar to duck typing is OCaml's structural subtyping, where object types are compatible if their method signatures are compatible, regardless of their declared inheritance. This is all detected at compile time through OCaml's type inference system.
In Python
Duck typing is heavily used in Python. The Python Tutorial's Glossary defines duck typing as follows:
Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using
type()
orisinstance()
. Instead, it typically employshasattr()
tests or EAFP (Easier to Ask Forgiveness than Permission) programming.
The standard example of duck typing in Python is file-like classes. Classes can implement some or all of the methods of file
and can be used where file
would normally be used. For example, GzipFile
implements a file-like object for accessing gzip-compressed data. cStringIO
allows treating a Python string as a file. Sockets and files share many of the same methods as well. However, sockets lack the tell()
method and cannot be used everywhere that GzipFile
can be used. This shows the flexibility of duck typing: a file-like object can implement only methods it is able to, and consequently it can be only used in situations where it makes sense.
In PHP
Duck typing in PHP is one of the basics of the language. A statement calling a method on an object does not rely on the type of the object, only that the object, of whatever type, must implement the method called.
In Ruby
Duck typing is a fundamental part of ruby coding. The pickaxe book (Programming Ruby), written by Dave Thomas, has a more complete description of duck typing, explaining its perils and benefits.
In Perl
Perl has duck typing built in on scalar (number or string) objects. For user-written code, duck typing can be performed using the can
method, which is available on all blessed objects in Perl.
In Java
In 2003, Dave Orme, leader of Eclipse's Visual Editor Project was looking for a generic way to bind any SWT (Standard Widget Toolkit) control to any JavaBeans-style object. He noticed that SWT reuses names religiously across its class hierarchy. For example, to set the caption of something, you normally set the Text property. This is true for an SWT Shell (window), a Text control, a Label, and many more SWT controls. Orme realized that if he could implement data binding in terms of the methods that are implemented on a control, he would save considerable work and achieve a much higher level of code reuse, compared to implementing separate data binding for an SWT Shell, Text, Label, and so on. When the Ruby community started describing this kind of type system as "duck typing", Orme realized that he had simply rediscovered what Smalltalk, Ruby, Python and other programmers had already known for a long time.
Orme formalized this knowledge by creating a class that makes duck typing simple and natural for Java programmers (see "Java Does Duck Typing"). Cedric Beust later cautioned about possible dangers using duck typing in "The Perils of Duck Typing".
Note that even in the absence of duck typing, you can still use the adapter pattern to make instances conform to given interfaces. The generation of adapters can be automated using dynamic code generation [1], a process that is also used in implementations of duck-typed languages on top of the JVM.
In Objective-C
In Objective-C you can refer to a class using the type(less) id. The RTE will map a message to such an object by resolving the adequate method without checking for a type. The question, whether an object has a method for a message is not a question of class.
You can send such a message explicitly or implicitly:
id anObject = … // An object of any class implementing -walkAndQuack [anObject walkAndQuack]; // implicit call [anObject performSelector:@selector( walkAndQuack )]; // explicit call
You can check at run-time, whether a particular method is implemented using -respondsToSelector:.
Cocoa is using this feature for example, when it asks several objects (Application, View, Window), whether a user interaction can be performed.
In ColdFusion
ColdFusion, a web application scripting language, also allows duck typing although the technique is still fairly new to the ColdFusion developer community. Function arguments can be specified to be of type any so that arbitrary objects can be passed in and then method calls are bound dynamically at runtime. If an object does not implement a called method, a runtime exception is thrown which can be caught and handled gracefully. An alternative argument type of WEB-INF.cftags.component restricts the passed argument to be a ColdFusion Component (CFC), which provides better error messages should a non-object be passed in.
Comparison with generics and structural subtyping
In C++ and some other languages, very flexible static binding capabilities, called generics, templates, and operator overloading, provide some of the same advantages, but typically not as late as run time. This static polymorphism is distinguished from runtime facilities for dynamic types.
External links
- Duck Typing: Ruby
- How to duck type? - the psychology of static typing in Ruby
- Java Does Duck Typing. 29 April 2005. (a Java Implementation of duck typing)
- The Perils of Duck Typing (in Java)
- Python documentation
- Using tell() on a socket with GzipFile
- NDuck: A DuckTyping Implementation for the .NET Framework (C#, VB.Net, ...)
- Guido van Rossum takes on strong vs. weak typing
- Duck Typing in RDF