Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative typing, an object is of a given type if it is declared to be (or if a type's association with the object is inferred through mechanisms such as object inheritance). In duck typing, an object is of a given type if it has all methods and properties required by that type.[1][2] Duck typing can be viewed as a usage-based structural equivalence between a given object and the requirements of a type. See structural typing for a further explanation of structural type equivalence.

ExampleEdit

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

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

    def fly(self):
        print("Duck flying")

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

for animal in [Duck(), Whale()]:
    animal.swim()
    animal.fly()

Output:

Duck swimming
Duck flying
Whale swimming
AttributeError: 'Whale' object has no attribute 'fly'

So, if we assume everything that can swim is a duck because ducks can swim, we will consider a whale to be a duck, but, if we also assume it has to be capable of flying, the whale won’t be considered to be a duck.

In statically typed languagesEdit

In some statically typed languages such as Boo[3] and D,[4][5] class type checking can be specified to occur at run time rather than compile time.

Comparison with other type systemsEdit

Structural type systemsEdit

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 TypeScript,[6] and Elm,[7] languages support structural typing to varying degrees.

Protocols and interfacesEdit

Protocols and interfaces provide a way to declare, explicitly, that some methods, operators, or behaviors need to be defined (e.g. must have a quack() method). 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. In contrast, under duck typing, the object would be accepted directly, without the need for an adaptor.

Templates or generic typesEdit

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 runtime need to be implemented, while templates require implementations of all methods that can not be proven unreachable at compile time.

In languages like Java, Scala, and Objective-C, reflection can be used to inspect whether objects implement methods or even add necessary methods at runtime.

For example, Java's MethodHandle API can be used in this manner.[8]

See alsoEdit

ReferencesEdit

  1. ^ "Glossary — Python 3.7.1 documentation". docs.python.org. Retrieved 2018-11-08.
  2. ^ "Python Duck Typing - Example". Techie Hours. 2020-06-28. Retrieved 2020-07-26.
  3. ^ Boo: Duck TypingArchived October 6, 2008, at the Wayback Machine
  4. ^ "Dynamic classes and duck typing".
  5. ^ "Metaprogramming - duck typing in D".
  6. ^ "SE Radio Episode 384: Boris Cherny on TypeScript". se-radio.net. Retrieved 2019-10-25.
  7. ^ Czaplicki, Evan. "Core Language · An Introduction to Elm". Retrieved 30 January 2017.
  8. ^ "StackOverflow: Implement duck typing using java MethodHandles". Retrieved 13 June 2020.