2

I'm trying to find out the differences between these types of polymorphism. I can't find much material on Ad-Hoc Polymorphism.

I want to say that Polymorphism is Parametric Polymorphism but is generally just referred to as Polymorphism? Am I correct to assume this?

I know that polymorphism can be used in inheritance but I'm not sure what other properties it can serve.

If anyone could help define these types of polymorphism and maybe try give the distinct differences, that would be great.

Thank you!

Note: An example of each in Python would be great.

Chris
  • 785
  • 10
  • 24
  • 2
    I answered a very similar question a few months. [Take a look](http://stackoverflow.com/a/11732581/697630). I think the reference it contains to Luca Cardeli's paper is what you're looking for. – Edwin Dalorzo Dec 13 '15 at 15:42
  • Thanks very much! I'll have a look now. – Chris Dec 13 '15 at 15:43

1 Answers1

7

Polymorphism is the general concept. Ad-hoc polymorphism and parametric polymorphism are specializations of the concept. (According to the relevant Wikipedia article, there also exist other types of polymorphism.)

Ad-hoc polymorphism is also known as function overloading, and it refers to using the type system in order to resolve precisely which method will be invoked. So, we may have two functions, both called fn, where one accepts an int parameter, while the other accepts a String parameter, and the right function to invoke is chosen based on the type of parameter being passed.

Parametric polymorphism is basically the use of generics. So, the Collection<T> interface can be said to be polymorphic because it can be used as Collection<Integer> and as Collection<String> and what not. The name "parametric" refers to the presence of generic parameters.

As far as I know, python does not have a strong concept of types, and neither does it support generics, ("templates" in C++ parlance,) so these concepts are probably inapplicable to Python. But, I have no practical experience with Python, so I may be wrong. Perhaps someone else can enlighten us.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Okay, thanks! So to make sure I understand it: Parametric polymorphism is the use of generics that allows any type to be passed to the function and handled by that function? In ML, I can do `fun gen x = x; val gen = fn : 'a -> 'a.` I can then call gen with an integer or real and it will handle it appropriately. I'm still pretty uncertain about adhoc but I'll have a look around and see if I can find any examples. Thanks! – Chris Dec 13 '15 at 17:01
  • @ChrisMulheron I am sorry, because my goal was to explain these things *clearly*, and obviously, I did not achieve this, if you are still left with questions. I don't know what ML is, (machine learning?) and I do not understand the `fun gen x = x; ...` syntax. Perhaps this Q&A will be helpful to you: http://stackoverflow.com/questions/6725868/generics-templates-in-python – Mike Nakis Dec 13 '15 at 17:14
  • Most chances are that you are having a hard time grasping these concepts because all this polymorphism comes for free with python, by virtue of using duck typing, so you can't see what the alternative would be. (Does a fish know it is wet?) You would understand if you were using a strongly typed language like C++, Java, C#, etc. – Mike Nakis Dec 13 '15 at 17:17
  • Don't worry about it, there's just so many different examples I'm probably just getting mixed up! I have an example: `x = 5 + 5; y = 5.0 + 5.0, print(x,y) => 10, 10.0;` Would this be an example of adhoc? the '+' operator is handling both an integer type and a float? In ML, you have to use two seperate division operators for integers('div') and reals('/'), would this be an example showing adhoc is not present in ML? Apologies if I'm being obscure. – Chris Dec 13 '15 at 17:40
  • Yes, precisely, the same operator being applicable to different types is polymorphism, while the requirement for different operators being necessary for different types is lack of polymorphism. – Mike Nakis Dec 13 '15 at 18:33
  • Okay great! Thanks very much :) – Chris Dec 13 '15 at 18:34
  • Interesting, Thanks for the answer , so if someone implemented a `__add__` or `__sub__` , do they fall under adhoc implementation of polymorphism or something else? – PKumar Apr 04 '22 at 08:35