Duck typing

Print Print
Reading time 5:27

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 normal typing, suitability is determined by an object's type. In duck typing, an object's suitability is determined by the presence of certain methods and properties, rather than the type of the object itself.[1][2]

Example

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 fly(self):
        print("Duck flying")

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

for animal in (Duck(), Whale()):
    animal.fly()

Output:

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

In statically typed languages

In some statically typed languages such as C# and Boo,[3][4] class type checking can be specified to occur at run time rather than compile time. Duck typing can be achieved in Java using the MethodHandle API.[5]

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

Protocols and interfaces

Protocols and interfaces may provide some of the benefits of duck typing, yet duck typing is distinct in not having an explicit interface 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 runtime need to be implemented, while templates require implementations of all methods that can not be proven unreachable at compile time.

Languages like Python, Java and Objective-C are examples of duck typing because it is possible in them to construct new types in runtime via reflection and inspect whether these objects implement certain methods. On the other hand, there are languages that rely on compile-time metaprogramming techniques (like C++ and its template system) and thus do not fit into the category of duck typing; instead, at some point in the compilation pipeline, all placeholder types become substituted with some concrete types specified in a particular instantiation. Even though certain type erasure is possible in them, runtime inspection is limited.

See also

References

  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. ^ "Anders Hejlsberg Introduces C# 4.0 at PDC 2008". Retrieved 30 January 2017.
  5. ^ "StackOverflow: Implement duck typing using java MethodHandles". Retrieved 13 June 2020.
  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.

By: Wikipedia.org
Edited: 2021-06-18 11:02:17
Source: Wikipedia.org