What does duck typing mean in software development?
Duck typing means that an operation does not formally specify the requirements that its operands have to meet, but just tries it out with what is given.
Unlike what others have said, this does not necessarily relate to dynamic languages or inheritance issues.
Example task: Call some method Quack
on an object.
Without using duck-typing, a function f
doing this task has to specify in advance that its argument has to support some method Quack
. A common way is the use of interfaces
interface IQuack {
void Quack();
}
void f(IQuack x) {
x.Quack();
}
Calling f(42)
fails, but f(donald)
works as long as donald
is an instance of a IQuack
-subtype.
Another approach is structural typing - but again, the method Quack()
is formally specified anything that cannot prove it quack
s in advance will cause a compiler failure.
def f(x : { def Quack() : Unit }) = x.Quack()
We could even write
f :: Quackable a => a -> IO ()
f = quack
in Haskell, where the Quackable
typeclass ensures the existence of our method.
Well, as I said, a duck typing system does not specify requirements but just tries if anything works.
Thus, a dynamic type system as Python's always uses duck typing:
def f(x):
x.Quack()
If f
gets an x
supporting a Quack()
, everything is fine, if not, it will crash at runtime.
But duck typing doesn't imply dynamic typing at all - in fact, there is a very popular but completely static duck typing approach that doesn't give any requirements too:
template <typename T>
void f(T x) { x.Quack(); }
The function doesn't tell in any way that it wants some x
that can Quack
, so instead it just tries at compile time and if everything works, it's fine.
It is a term used in dynamic languages that do not have strong typing.
The idea is that you don't need to specify a type in order to invoke an existing method on an object - if a method is defined on it, you can invoke it.
The name comes from the phrase "If it looks like a duck and quacks like a duck, it's a duck".
Wikipedia has much more information.
“If it walks like a duck and quacks like a.... etc” - YES, but what does that mean??!
We're interested in what "objects" can do, rather than what they are.
Let's unpack it with an example:
See below for further detail:
Imagine I have a magic wand. It has special powers. If I wave the wand and say "Drive!" to a car, well then, it drives!
Does it work on other things? Not sure: so I try it on a truck. Wow - it drives too! I then try it on planes, trains and 1 Woods (they are a type of golf club which people use to 'drive' a golf ball). They all drive!
But would it work on say, a teacup? Error: KAAAA-BOOOOOOM! that didn't work out so good. ====> Teacups can't drive!! duh!?
This is basically the concept of duck typing. It's a try-before-you-buy system. If it works, all is well. But if it fails, like a grenade still in your hand, it's gonna blow up in your face.
In other words, we are interested in what the object can do, rather than with what the object is.
If we were concerned with what the object actually was, then our magic trick will work only on pre-set, authorised types - in this case cars, but will fail on other objects which can drive: trucks, mopeds, tuk-tuks etc. It won't work on trucks because our magic wand is expecting it to only work on cars.
In other words, in this scenario, the magic wand looks very closely at what the object is (is it a car?) rather than what the object can do (e.g. whether cars, trucks etc. can drive).
The only way you can get a truck to drive is if you can somehow get the magic wand to expect both trucks and cars (perhaps by "implementing a common interface"). If you don't know what that means, check out my cartoon on interfaces.
What's important in duck typing is what the object can actually do, rather than what the object is.
I tried to keep it simple, cutting out pedantic nuances. If you want a more precise / academic definition check out the wikipedia article to duck typing, or Matt Damon's explanation to duck typing in Good Will Hunting
Consider you are designing a simple function which gets an object of type Bird
and calls its walk()
method. There are two approaches you can think of:
Bird
type or the code will not compile. If anyone wants to use my function, they must be aware that I only accept Bird
s.objects
and I just call the object's walk()
method. So, if the object
can walk()
then it is correct. If it can't, my function will fail. So, here it is not important the object is a Bird
or anything else, it is important that it can walk()
(This is duck typing).It must be considered that duck typing may be useful in some cases. For example, Python uses duck typing a lot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With