Wikipedia used to say* about duck-typing:
In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.
(* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".)
It says about structural typing:
A structural type system (or property-based type system) is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations.
It contrasts structural subtyping with duck-typing as so:
[Structural systems] contrasts with ... duck typing, in which only the part of the structure accessed at runtime is checked for compatibility.
However, the term duck-typing seems to me at least to intuitively subsume structural sub-typing systems. In fact Wikipedia says:
The name of the concept [duck-typing] refers to the duck test, attributed to James Whitcomb Riley which may be phrased as follows: "when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."
So my question is: why can't I call structural subtyping duck-typing? Do there even exist dynamically typed languages which can't also be classified as being duck-typed?
Postscript:
As someone named daydreamdrunk on reddit.com so eloquently put-it "If it compiles like a duck and links like a duck ..."
Post-postscript
Many answers seem to be basically just rehashing what I already quoted here, without addressing the deeper question, which is why not use the term duck-typing to cover both dynamic typing and structural sub-typing? If you only want to talk about duck-typing and not structural sub-typing, then just call it what it is: dynamic member lookup. My problem is that nothing about the term duck-typing says to me, this only applies to dynamic languages.
There are situations in which dynamic duck typing and the similar static-typed code (in i.e. C++) behave differently:
In C++, the object must have both the
isAlive
andquak
methods for the code to compile; for the equivalent code in dynamically typed languages, the object only needs to have thequak
method ifisAlive()
returns true. I interpret this as a difference between structure (structural typing) and behavior (duck typing).(However, I reached this interpretation by taking Wikipedia's "duck-typing must be dynamic" at face value and trying to make it make sense. The alternate interpretation that implicit structural typing is duck typing is also coherent.)
It's my understanding that structural typing is used by type inferencers and the like to determine type information (think Haskell or OCaml), while duck typing doesn't care about "types" per se, just that the thing can handle a specific method invocation/property access, etc. (think
respond_to?
in Ruby or capability checking in Javascript).Duck typing means If it just fits, it's OK
This applies to both dynamically typed
or statically typed, compiled languages
The point is that in both examples, there has not been any information on the type given. Just when used (either at runtime or compile-time!), the types are checked and if all requirements are fulfilled, the code works. Values don't have an explicit type at their point of declaration.
Structural typing relies on explicitly typing your values, just as usual - The difference is just that the concrete type is not identified by inheritance but by it's structure.
A structurally typed code (Scala-style) for the above example would be
Don't confuse this with the fact that some structurally typed languages like OCaml combine this with type inference in order to prevent us from defining the types explicitly.
Structural Type System
A structural type system compares one entire type to another entire type to determine whether they are compatible. For two types
A
andB
to be compatible,A
andB
must have the same structure – that is, every method onA
and onB
must have the same signature.Duck Typing
Duck typing considers two types to be equivalent for the task at hand if they can both handle that task. For two types
A
andB
to be equivalent to a piece of code that wants to write to a file,A
andB
both must implement a write method.Summary
Structural type systems compare every method signature (entire structure). Duck typing compares the methods that are relevant to a specific task (structure relevant to a task).
C++ and D templates are a perfect example of duck typing that is not dynamic. It is definitely:
You don't explicitly specify an interface that your type must inherit from to instantiate the template. It just needs to have all the features that are used inside the template definition. However, everything gets resolved at compile time, and compiled down to raw, inscrutable hexadecimal numbers. I call this "compile time duck typing". I've written entire libraries from this mindset that implicit template instantiation is compile time duck typing and think it's one of the most under-appreciated features out there.
There are always going to be examples from some programming languages that violate some definitions of various terms. For example, ActionScript supports doing duck-typing style programming on instances that are not technically dynamic.
In this case we tested if the object instance in "x" has a method "begin" before calling it instead of using an interface. This works in ActionScript and is pretty much duck-typing, even though the class SomeClass() may not itself be dynamic.