Duck Typing

In computer programming, duck typing is an application of the duck test in type safety. It requires that type checking be 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.[]


This is a simple example in Python 3 that demonstrates how any object may be used any context, up until it is used in a way that it does not support.

class Sparrow:
    def fly(self):
        print("Sparrow flying")

class Airplane:
    def fly(self):
        print("Airplane flying")

class Whale:
    def swim(self):
        print("Whale swimming")

def lift_off(entity):

sparrow = Sparrow
airplane = Airplane
whale = Whale

lift_off(sparrow) # prints `Sparrow flying`
lift_off(airplane) # prints `Airplane flying`
lift_off(whale) # Throws the error `'Whale' object has no attribute 'fly'`

In statically typed languages

Certain usually statically typed languages such as Boo and the version 4 release of C# have extra type annotations[1][2] 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.

The OCaml, Scala, Go, Elm,[3] and Gosu languages support structural typing to varying degrees.

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 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 does in fact satisfy the interface requirements. (A common solution to this problem is the Adapter pattern.) 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.

Examples include the languages C++ and D with templates, which developed from Ada generics.


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.[4] To proponents of static type checking, duck typing suggests the absence of typing, making its incorporation of the term typing appear incoherent.


Alex Martelli made an early (2000) use of the term in a message to the comp.lang.python newsgroup:

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.

See also


  1. ^ Boo: Duck Typing Archived October 6, 2008, at the Wayback Machine.
  2. ^ "Anders Hejlsberg Introduces C# 4.0 at PDC 2008". Retrieved 2017. 
  3. ^ Czaplicki, Evan. "Core Language · An Introduction to Elm". Retrieved 2017. 
  4. ^ Lippert, Eric (2 Jan 2014). "What is "duck typing"?". Fabulous adventures in coding. Retrieved 2016. ... the whole idea of duck typing is fundamentally incoherent ...' 

External links

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your IT knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us