18

From Polymorphism on WIkipedia

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.

From duck typing on Wikipedia

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.

My interpretation is that based on duck typing, the objects methods/properties determine the valid semantics. Meaning that the objects current shape determines the interface it upholds.

From polymorphism you can say a function is polymorphic if it accepts multiple different data types as long as they uphold an interface.

So if a function can duck type, it can accept multiple different data types and operate on them as long as those data types have the correct methods/properties and thus uphold the interface.

(Usage of the term interface is meant not as a code construct but more as a descriptive, documenting construct)

  • What is the correct relationship between ducktyping and polymorphism ?
  • If a language can duck type, does it mean it can do polymorphism ?
4
  • 1
    Not sure what you are looking for in an answer. You defined both accurately, so you have as much of a definitive answer as there is. Deciding if duck typing "counts" as polymorphism is more or less a philosophical question, or maybe a terminology question if you mean it that way. So what would you be looking for in an answer?
    – psr
    Nov 25, 2011 at 23:11
  • @psr I was basically saying "here's how I interpret it. Am I wrong? Am I right? Are there repubable academic people that say it it one way or the other. Are there any articles on the topic?" Basically in detail other then "duck typing is an implementation / subset of polymorphism" what else can be said about the relationship?
    – Raynos
    Nov 25, 2011 at 23:15
  • as far as I understand homography makes duck typing anything else but uniform interface => not a polymorphism, or at least not in a sense of how Wikipedia defines it. Eg door.close() and tiger.close()
    – gnat
    Nov 25, 2011 at 23:19
  • 4
    Duck typing is a case of Ad-hoc polymorphism. You're over-thinking this.
    – yannis
    Nov 26, 2011 at 22:20

6 Answers 6

18

I say that polymorphism is a generic trait, that can be implemented several ways:

  • class based inheritance.
  • prototype based objects (with or without inheritance)
  • duck typing
  • interface compliance (as done by Go's interfaces and implicitly on C++ templates)

each of them allows the programmer to use a single code with different types, so all embody the concept of polymorphism.

7

I think that:

Both duck typing and polymorphism are means of indirection/abstraction. Polymorphism is a concept founded on types and typing whereas duck typing is founded on contracts.

With polymorphism it is important what the THING is and not how it behaves (it behavior may be the consequence of what it is).

In duck typing it is important how a THING behaves. Duck typing is more tied to the concept of objects as actors that exchange messages, rather than objects which have some defined properties.

2
  • 3
    This is wrong. Polymorphism is a huge umbrella covering lots of things. In particular, it covers duck typing. Thus duck typing is a form of polymorphism. Aug 21, 2014 at 23:01
  • I disagree. Traditional inheritance with behavioural methods has the same traits. It's just more type-safe by being explicit as to what behaviour an object is guaranteed to exhibit (which does not exclude other behaviours!)
    – marstato
    Oct 17, 2018 at 9:32
6

The answer is YES.

Duck Typing is a special case of dynamic typing, and even if dynamic typing per se cannot be directly considered as polymorphism (since it is just the property of the language to perform type check at runtime rather then at compile time), the techniques that usually underlie the dynamic typing, such late binding and dynamic dispatch are characteristic for polymorphism.

1
  • 1
    The problem with the Wikipedia article and how many people use the term nowadays is that it doesn't define what duck typing meant when I first saw it (namely, a form of typing) but rather, being able to just invoke methods and throw a runtime error when they are not defined. That is not typing but rather the lack of typing, as Eric Lippert explains. Jun 26, 2015 at 15:21
1

Duck typing is not quite a subset of polymorphism, since polymorphism requires explicitness, which is lost in duck typing. So duck typing has the chance of "implementing" an "interface" not because it makes semantic sense but because you have the same signature. If duck typing were a subset of polymorphism then it would have all the properties of polymorphism, including explicit declaration.

Duck typing also isn't really an implementation of polymorphism, it's part of a different type system. Usually you associate duck typing with a dynamic language that goes ahead and passes a message to an object without knowing whether the object can handle it - if it can then it has effectively passed the duck typing test. Usually you think of polymorphism as being implemented at compile time by setting up vtables (and itables if interfaces are different than classes). But there are LOTS of languages out there and lots of ways to implement these features.

To some extent this is a philosophical question. Can you think of duck typing as interfaces that are automatically declared implicitly? I can't think of any reason that's wrong, exactly, but I don't think it's probably the most productive way to look at it. I think duck typing and interfaces are both commonly encountered features of programming language's type systems, both have fairly similar ways of behaving, and both are important for programmers to understand.

6
  • 7
    "polymorphism requires explicitness" where does it say so? class-based inheritance is just a form of polymorphism, not the only one.
    – Javier
    Nov 26, 2011 at 11:17
  • And continuing @Javier's comment, how about ad hoc polymorphism?
    – yannis
    Nov 26, 2011 at 22:12
  • @YannisRizos - The problem with answering any question like this is there are so many languages and so much terminology that someone might nitpick you no matter what you say. I understand why "ad hoc polymorphism" has the word "polymorphism" in the name, but I would argue it's something else than what the OP means by "polymorphism".
    – psr
    Nov 28, 2011 at 15:59
  • @Javier - All the methods you listed require explicitness except duck typing.
    – psr
    Nov 28, 2011 at 16:04
  • 1
    @psr Actually I don't care about what OP meant by "polymorphism". I don't think anyone should care, it's a scientific concept with a very simple definition, I don't think we should answer based on our own interpretations or what we may think it's the OPs interpretation. Especially the OPs interpretation, since he is asking so by default he is unsure about his interpretation. I disagree that's it's a philosophical question, the question as stated was a scientific one with one simple answer: Yes ducktyping is a form of ad hoc polymorphism, one subset of polymorphism.
    – yannis
    Nov 28, 2011 at 21:14
1

It feels almost natural to say "Yes, if foo has duck typing, foo has polymorphism". But I can't say this 100% sure, in a sense that maybe it is possible to come up with an artificial examples of such system which has duck typing ("can it quack && can it float on water ==> it is a Duck") while not having polymorphism ("foo, quack!" fails), but they would really be artificial and in a real world, I'd say "Yes, if duck typing is present, polymorphism must also be present".

Personally I see duck typing as "polymorphism done right". What I mean by that, thing that exist in a duck typing world do not need to have any explicit types and their (polymorphic = "same access, different outcome") behaviour is the only thing that counts. In other implementations of polymorphism, it is constrained to types/interfaces/inheritance, so it is "implemented and constrained polymorphism" not a "polymorphism per se".

0

Statically typed programming languages allow earlier error checking, better enforcement of disciplined programming styles, and generation of more efficient object code than languages where all type consistency checks are performed at run time.

(B. Pierce & friends)

So as you can note, in this regard, duck typing is dynamic typing a really run otherwise of these.

There is some method to determine type that relate to the polymorphing e.g. that makes code more flexible but duck typing is another issue altogether in these cases.

In details there is typed lambda calculus and untyped lambda calculus that help determine the property of calculation and a computation.

I also see that things like duck typing can help if we just want to get a result fast but on other occasions I also feel that the dynamic nature makes it more adjustable to needs, thus I can compute faster. I guess that is just to say, dear, that if I know really what I want to compute already, typed is best, but why do I know what I want? Hehehe ...

That's my five cents and I guess that can make an interesting research topic.

Not the answer you're looking for? Browse other questions tagged or ask your own question.