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. The analogy to the duck test appeared in a Python Enhancement Proposal discussion in 2002.[1]

Concept examples

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 ([1], [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,

Thus, duck typing can work the same as polymorphism, but without inheritance. The only restriction that function calculate places on its arguments is that they implement the "+" and the "*" methods.

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[2][3] 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,[4] 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 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

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

History

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.

Language support

In C++

class Duck {
public:
    void quack() {
        std::cout << "Quaaaaaack!" << std::endl;
    }
    
    void feathers() {
        std::cout << "The duck has white and gray feathers." << std::endl;
    }
}

class Person {
public:
    void quack() {
        std::cout << "The person imitates a duck." << std::endl;
    }
    
    void feathers() {
        std::cout << "The person takes a feather from the ground and shows it." << std::endl;
    }
}

template <typename T>
void inTheForest(T& t)
{
    t.quack();
    t.feathers();
}

int main()
{
    Duck donald;
    Person john;
    
    inTheForest( donald );
    inTheForest( john );
}

In C#

As of C# 4.0 the compiler and runtime collaborate to implement dynamic member lookup.

In the following C# 6.0 code, the parameter duck of the method Program.InTheForest is declared as dynamic.

using static System.Console;

class Duck
{
	public void Quack() => WriteLine("Quaaaaaack!");
	public void Feathers() => WriteLine("The duck has white and gray feathers.");
}

class Person
{
	public void Quack() => WriteLine("The person imitates a duck.");
	public void Feathers() => WriteLine("The person takes a feather from the ground and shows it.");
}

class Program
{
	static void InTheForest(dynamic duck)
	{
		duck.Quack();
		duck.Feathers();
	}

	static void Game()
	{
		Duck donald = new Duck();
		Person john = new Person();
		InTheForest(donald);
		InTheForest(john);
	}

	static void Main()
	{
		Game();
	}
}

In CFML

The web application scripting language CFML allows function arguments to be specified as having type any. For this sort of argument, an arbitrary object can be passed in and method calls are bound dynamically at runtime. If an object does not implement a called method, a runtime exception is thrown that can be caught and handled gracefully. In ColdFusion 8, this can be picked up as a defined event onMissingMethod() rather than through an exception handler. 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.

Other CFML application servers such as Lucee work analogously to ColdFusion's CFML implementation.

In Dart

class Duck {
	quack() => print("Quack, quack!");
	fly()	=> print("Flap, Flap!");
}

class Person {
	quack() => print("I'm Quackin'!");
	fly()	=> print("I'm Flyin'!");
}

inTheForest(mallard) {
	mallard.quack();
	mallard.fly();
}

main() {
	inTheForest(new Duck());
	inTheForest(new Person());
}

In Cobra

In addition to static typing, Cobra allows one to declare objects of type 'dynamic' and send any message to them. At run-time the message passing will either succeed or throw an exception. The 'dynamic' type is the default for object variables and method arguments when a type has not been explicitly declared for them. This feature was inspired by Objective-C.[6]

In Common Lisp

Common Lisp includes an object-oriented system (Common Lisp Object System, or shorter CLOS) providing classes with multiple inheritance and generic functions that can specialize on multiple arguments. The combination of CLOS and Lisp's dynamic typing make duck typing a common programming style in Common Lisp.

With Common Lisp one also does not need to query the types, since at runtime an error will be signaled when a generic function is not applicable. The error can be handled with the condition system of Common Lisp. Methods are defined outside of classes and can also be defined for specific objects.

;; We describe a protocol for 'duck-like' objects. Objects with methods for
;; these three generic functions may be considered 'ducks', for all intents
;; and purposes -- regardless of their superclass.
(defgeneric quack (something))
(defgeneric feathers (something))

;; Implementation of the protocol for class DUCK.
(defclass duck () ())

(defmethod quack ((a-duck duck))
	(print "Quaaaaaack!"))

(defmethod feathers ((a-duck duck))
	(print "The duck has white and gray feathers."))

;; But we can also implement it for PERSON, without inheriting from DUCK.
(defclass person () ())

(defmethod quack ((a-person person))
	(print "The person imitates a duck."))

(defmethod feathers ((a-person person))
	(print "The person takes a feather from the ground and shows it."))

;; IN-THE-FOREST does not need to be polymorphic. Its 'duck' argument is
;; anything that implements the duck protocol above.
(defun in-the-forest (duck)
	(quack duck)
	(feathers duck))

;; GAME can also just be a regular function.
(defun game ()
	(let ((donald (make-instance 'duck))
		(john (make-instance 'person)))
	(in-the-forest donald)
	(in-the-forest john)))

(game)

The usual development style of Common Lisp (by using a Lisp REPL like SLIME) allows also the interactive repair:

? (defclass cat () ())
#<STANDARD-CLASS CAT>
? (quack (make-instance 'cat))
> Error: There is no applicable method for the generic function:
>      #<STANDARD-GENERIC-FUNCTION QUACK #x300041C2371F>
>    when called with arguments:
>      (#<CAT #x300041C7EEFD>)
> If continued: Try calling it again
1 > (defmethod quack ((a-cat cat))
                (print "The cat imitates a duck."))

#<STANDARD-METHOD QUACK (CAT)>
1 > (continue)

"The cat imitates a duck."

This way software can be developed by extending partially working duck typed code.

In Go

In Go, any type that has the methods required for an interface can act as that interface, regardless of whether or not it was explicitly defined or intended. Because of this, multiple sources use the term duck typing when describing Go interfaces.[7][8] However, Go typically checks for compatibility at compile-time, which is why the Go designers describe the interfaces as a form of structural typing.[9]

Although in general duck typing can be implemented with reflection, the support for reflection in Go is limited. Calling methods through reflection requires the use of interface type assertions.[10] The runtime validates that the underlying type of a value supports all methods defined in the given interface. In duck typing, the runtime validates each method invokation separately.

In Groovy

In Groovy, the Java-derived scripting language, the below Java example can be greatly simplified, because Groovy uses duck typing by default when calling a method.

class Duck
{
	def walk() { println "I'm a Duck, I can walk..." }
	def swim() { println "I'm a Duck, I can swim..." }
	def quack() { println "I'm a Duck, I can quack" }
}

class Person
{
	def walk() { println "I'm a Person, I can walk..." }
	def swim() { println "I'm a Person, I can swim..." }
	def talk() { println "I'm a Person, I can talk..." }
}

def d = new Duck()
def p = new Person()
		
d.walk()	// Ok, duck has walk() method
d.swim()	// Ok, duck has swim() method
d.quack()	// Ok, duck has quack() method

p.walk()	// Ok, person has walk() method
p.swim()	// Ok, person has swim() method
p.quack()	// Runtime error, no quack() method

In Java

In Java duck typing may be achieved with reflection.

public class DuckTyping {

	public static void main(String[] args) {
		Duck duck = new Duck();
		Person person = new Person();

		try {
			actAsADuck(duck);
			actAsADuck(person);
		} catch (NoSuchMethodException e) {
			System.out.println("Method not found: " + e.getMessage());
		} catch (Exception e) {
			System.out.println("Execution failed: " + e);
		}
	}

	static void actAsADuck(Object obj) throws Exception {
		Class<?> objClass = obj.getClass();
		objClass.getDeclaredMethod("walk").invoke(obj);
		objClass.getDeclaredMethod("swim").invoke(obj);
		objClass.getDeclaredMethod("quack").invoke(obj);
	}
}

class Duck {
	public void walk()	{System.out.println("I'm a Duck, I can walk...");}
	public void swim()	{System.out.println("I'm a Duck, I can swim...");}
	public void quack()	{System.out.println("I'm a Duck, I can quack...");}
}

class Person {
	public void walk()	{System.out.println("I'm a Person, I can walk...");}
	public void swim()	{System.out.println("I'm a Person, I can swim...");}
	public void talk()	{System.out.println("I'm a Person, I can talk...");}
}

Running the DuckTyping class will produce the following output:

I'm a Duck, I can walk...
I'm a Duck, I can swim...
I'm a Duck, I can quack...
I'm a Person, I can walk...
I'm a Person, I can swim...
Method not found: Person.quack()

In JavaScript

function Duck() {
  // Dynamically add functions to this object
  this.quack = function() { alert('Quaaaaaack!'); };
  this.feathers = function() { alert('The duck has white and gray feathers.'); };
}

function Person() {
  // Dynamically add functions to this object
  this.quack = function() { alert('The person imitates a duck.'); };
  this.feathers = function() { alert('The person takes a feather from the ground and shows it.'); };
  this.name = function() { alert('John Smith'); };
}

function inTheForest(object) {
  if (object.quack)     // Check that the .quack() function exists
    object.quack();
  if (object.feathers)  // Check that the .feathers() function exists
    object.feathers();
}

function game() {
  var donald = new Duck();
  var john = new Person();
  inTheForest(donald);
  inTheForest(john);
}

// Execute upon page load
game();

The function inTheForest() above checks that the object passed to it has a member quack and a member feathers before attempting to invoke them. More explicit tests could also be made, wherein the runtime types of the members are also checked:

function inTheForest(object) {
  if (object.quack  &&  typeof(object.quack) == 'function')        // Check that the .quack() function exists
    object.quack();
  if (object.feathers  &&  typeof(object.feathers) == 'function')  // Check that the .feathers() function exists
    object.feathers();
}

In Julia

Julia uses multiple dispatch, generic functions, optional type annotations and automatic type inference by default, the type Any is the super-type of the whole hierarchy.

type Duck end

type Person
    name::ASCIIString
end

quack(x::Duck) = println("Quaaaaaack!")
quack(x::Person) = println("The person imitates a duck.")

feathers(x::Duck) = println("The duck has white and gray feathers.")
feathers(x::Person) = println("The person takes a feather from the ground and shows it.")

name(x::Person) = println(x.name)

function in_the_forest(duck)    # same as duck::Any
    quack(duck)
    feathers(duck)
end

function game()
    donald = Duck()
    john = Person("John Smith")

    in_the_forest(donald)
    in_the_forest(john)
end

game()

In Lua

Lua supports duck typing as part of the Metatable weak-typing system. Any reference to a table's member function is checked dynamically at run-time. If an object does not implement the requested function, a run-time error is produced. If a data member is requested, but does not exist, a nil value is returned.

local duck_mt = {}
local duck_methods = {}
duck_mt.__index = duck_methods

function duck_methods:quack()
	print "Quaaaaaack!"
end

function duck_methods:feathers()
	return "The duck has white and gray feathers."
end

local function new_duck()
	return setmetatable({}, duck_mt)
end

local person_mt = {}
local person_methods = {}
person_mt.__index = person_methods

function person_methods:quack()
	print "The person imitates a duck."
end

function person_methods:feathers()
	return "The person takes a feather from the ground and shows it."
end

function person_methods:get_name()
	return self.firstname .. " " .. self.lastname
end

local function new_person(t)
	return setmetatable(t or {}, person_mt)
end

local function in_the_forest(duck)
	duck:quack()
	print(duck:feathers())
end

local donald = new_duck()
local john = new_person {firstname="John", lastname="Smith"}
in_the_forest(donald)
in_the_forest(john)

Output:

Quaaaaaack!
The duck has white and gray feathers.
The person imitates a duck.
The person takes a feather from the ground and shows it.

In Objective-C

Objective-C, a cross between C and Smalltalk, allows one to declare objects of type 'id' and send any message to them (provided the method is declared somewhere), like in Smalltalk. The sender can test an object to see if it responds to a message, the object can decide at the time of the message whether it will respond to it or not, and, if the sender sends a message a recipient cannot respond to, an exception is raised. Thus, duck typing is fully supported by Objective-C.

@import Foundation;

@interface Duck : NSObject
@end

@implementation Duck
- (void)quack { NSLog(@"Quaaaack!"); }
@end

@interface Person : NSObject
@end

@implementation Person
- (void)quack { NSLog(@"The person imitates a duck."); }
@end

@interface Dog : NSObject
@end

@implementation Dog
- (void)bark { NSLog(@"Baaaaark!"); }
@end

void inTheForest(id duck) {
    if ([duck respondsToSelector:@selector(quack)]) {
        [duck quack];
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        inTheForest([[Duck alloc] init]);
        inTheForest([[Person alloc] init]);
        inTheForest([[Dog alloc] init]);
    }
    return 0;
}

Output:

Quaaaack!
The person imitates a duck.

In Perl

Perl looks for method definitions in package set with bless function.

use strict;

package Duck;

sub hatch {
	bless \(my $self), shift;
}
sub quack {
	print "Quaaaaaack!\n";
}
sub feathers {
	print "The duck has white and gray feathers.\n";
}

package Person;

sub accept_birth {
	bless \(my $self), shift;
}
sub quack {
	print "The person imitates a duck.\n";
}
sub feathers {
	print "The person takes a feather from the ground and shows it.\n";
}

package main;

sub in_the_forest
{
	my $duck = shift;
	$duck->quack();
	$duck->feathers();
}

my $duck = Duck->hatch();
my $person = Person->accept_birth();

in_the_forest( $duck );
in_the_forest( $person );

Output:

Quaaaaaack!
The duck has white and gray feathers.
The person imitates a duck.
The person takes a feather from the ground and shows it.

In PHP

PHP leans towards the Java convention of using inheritance and the user land type system (type hinting method arguments or using instanceof class or interface) in favour of duck typing. Below is an example of duck typing:

<?php
class Duck {
    function quack() { echo "Quack", PHP_EOL; }
    function fly() { echo "Flap, Flap", PHP_EOL; }
}

class Person {
    function quack() { echo "I try to imitate a duck quack", PHP_EOL; }
    function fly() { echo "I take an airplane", PHP_EOL; }
}

function in_the_forest($object) {
    $object->quack();
    $object->fly();
}

in_the_forest(new Duck);
in_the_forest(new Person);

Output:

Quack
Flap, Flap
I try to imitate a duck quack
I take an airplane

In PowerShell

This is the concept example from the beginning of the page.

Function calculate($a, $b, $c) {
    return ($a + $b)*$c
}
PS  > calculate 1 2 3
9
PS  > "$(calculate (1, 2, 3) (4, 5, 6) 2)"
1 2 3 4 5 6 1 2 3 4 5 6
PS  > calculate 'apples ' 'and oranges, ' 3
apples and oranges, apples and oranges, apples and oranges,

In Python

Duck typing is heavily used in Python, with the canonical example being file-like classes (for example, cStringIO allows a Python string to be treated as a file).

class Duck:
    def quack(self):
        print("Quack, quack!");

    def fly(self):
        print("Flap, Flap!");

class Person:
    def quack(self):
        print("I'm Quackin'!");

    def fly(self):
        print("I'm Flyin'!");

def in_the_forest(mallard):
    mallard.quack()
    mallard.fly()

in_the_forest(Duck())
in_the_forest(Person())

Output:

Quack, quack!
Flap, Flap!
I'm Quackin'!
I'm Flyin'!

According to the EAFP principle, instead of checking to see if some purportedly Duck-like object has a quack() method (using if hasattr(mallard, "quack"): ...), it's usually preferable to wrap the attempted quack with proper exception handling:

try:
    mallard.quack()
except (AttributeError, TypeError):
    print("mallard can't quack()")

Or, a more common use of the principle is to just let the exception "bubble up", that is, to let the exception be raised, and let whatever function or method called the code in question deal with it (or, if nothing deals with it, to let the exception be raised to the user). This gives better feedback on bad input, and avoids masking bugs.

In Ruby

class Duck
  def quack
    puts "Quaaaaaack!"
  end
	
  def feathers
    puts "The duck has white and gray feathers."
  end
end

class Person
  def quack
    puts "The person imitates a duck."
  end
	
  def feathers
    puts "The person takes a feather from the ground and shows it."
  end
end

def in_the_forest(duck)
  duck.quack
  duck.feathers
end

def game
  donald = Duck.new
  john = Person.new
  in_the_forest donald
  in_the_forest john
end

game

Output:

Quaaaaaack!
The duck has white and gray feathers.
The person imitates a duck.
The person takes a feather from the ground and shows it.

In Smalltalk

Duck typing is fundamental to Smalltalk. Variables have no data type, and can hold any object. Behavior is triggered by messages sent between objects. Any arbitrary string can be sent to any object as a message. The receiving object checks its method list for a matching behavior. This is the only approximation of type-checking in the language.

Moreover, a message with no matching method is not necessarily an error. In this case, the receiving object triggers its own doesNotUnderstand: method, inherited from Object. The default implementation raises an error, but this can be overridden to perform arbitrary operations based on the original message.

In Typescript

Typescript is unusual, in that it is a statically-typed language which uses duck-typing pervasively at compile time. Compile-time type checking is always based on type signatures, never inheritance. Run time type checking is done using the "instanceof" operator, which is not duck-typed.

This is similar to Go's "structural typing", but it applies to both classes and interfaces. Unlike Go, Typescript has an "implements" keyword to declare that a type should implement a particular interface. It is not required—if a class implements all the members of interface IFoo, it can be used as an IFoo. An explicit "implements" keyword provides compile-time errors if a developer fails to correctly implement an interface which he or she had intended to.

In the following example, it is legal to assign a Seagull object to a Duck variable, even though neither class inherits from the other. Because Seagull implements both talk() and swim() methods, it can be used as a Duck. A Parrot object cannot be assigned to a Duck variable because it does not implement any swim() method. A Fish object cannot be assigned to a Duck variable. Even though it implements both talk() and swim() methods, the talk() method has the wrong signature (returning a void rather than a string).

class Duck {
    public talk(): string { return "Quack!"; }
    public swim(): void { console.log("swimming"); }
}

class Seagull {
    public talk(): string { return "Screech!"; }
    public swim(): void { console.log("swimming"); }
}

class Parrot {
    public talk(): string { return "Polly wants a cracker!"; }
}

class Fish {
    public talk(): void { return; }
    public swim(): void { console.log("swimming"); }
}

var duck: Duck;

// OK
duck = new Duck();

// OK
duck = new Seagull();

// Compile time error: Type 'Parrot' is not assignable to type 'Duck'.
// Property 'swim' is missing in type 'Parrot'.
duck = new Parrot();

// Compile time error: Type 'Fish' is not assignable to type 'Duck'.
// Type '() => void' is not assignable to type '() => string'.
duck = new Fish();

See also

References

  1. "PEP 285: Adding a bool type".
  2. Boo: Duck Typing
  3. Anders Hejlsberg Introduces C# 4.0 at PDC 2008
  4. http://guide.elm-lang.org/core_language.html
  5. 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 ...'
  6. "Cobra - Acknowledgements". cobra-language.com. Retrieved 2010-04-07.
  7. Diggins, Christopher (2009-11-24). "Duck Typing and the Go Programming Language". Dr. Dobb's. Retrieved 2016-03-10.
  8. Ryer, Mat (2015-12-01). "Duck typing in Go". Retrieved 2016-03-10.
  9. "Why doesn't Go have "implements" declarations?". golang.org. Retrieved 1 October 2015.
  10. Rob Pike. "The Laws of Reflection". golang.org.

External links

This article is issued from Wikipedia - version of the 11/26/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.