Skip to content

Duck typing

wikipedia Duck test

The duck test is a form of abductive reasoning(翻译推理). This is its usual expression:

If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

The test implies that a person can identify an unknown subject by observing that subject's habitual characteristics. It is sometimes used to counter abstruse arguments that something is not what it appears to be.

wikipedia Duck typing

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 if 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]

Comparison with other type systems

Templates or generic types

NOTE: 这部分内容放到了Theory\Programming-paradigm\Generic-programming\Templates-VS-Duck-Typing-VS-Generics章节。