Does OCaml have polymorphism?
Asked Answered
P

1

6

Since OCaml has different operations for multiplying integers and doubles, how it can be object-oriented? Doesn't this mean OCaml has no polymorphism?

Photodynamics answered 20/1, 2013 at 12:37 Comment(2)
As sepp2k mentioned there are different kind of polymorphism if you want to better understand these thing is suggest this paper which can be freely downloaded here.Jorgenson
this may help fdatamining.blogspot.com/2012/12/…Robeson
K
11

Yes, OCaml has polymorphism. Here are some reasons why arithmetic operations aren't implemented as polymorphic methods:

  1. Integers and doubles aren't objects in OCaml, so they can't have methods.
  2. Operators in OCaml are functions, not methods. Of course the language could have been designed to make them methods instead, but then you could not define custom operators for anything that isn't an object.
  3. If you write a function like f (x, y) = x + y the inferred type is int * int -> int. If you changed the language to make operators methods and ints objects, the inferred type would be < + : 'a -> 'b; .. > * 'a -> 'b. Having such a complicated type for such a simple function would probably not be desirable.
  4. Paying the cost of polymorphic dispatch every time an arithmetic operation is used would be bad for performance.

Also note that in many mainstream languages that support operator overloading, operators tend to be implemented as non-virtual (and thus non-polymorphic) methods or functions as well. Presumably for the performance reason I mentioned above. Having polymorphic operators is rather uncommon.

PS: In the context of functional languages the term "polymorphism" is most often used to refer to "parametric polymorphism" (what OO languages sometimes call "generics"), while in OO languages it is most often used to refer to "subtype polymorphism". This answer assumes that you were using the latter meaning of the word since you explicitly mentioned object orientation and since the former meaning doesn't really make sense in this context.

Karisakarissa answered 20/1, 2013 at 12:50 Comment(7)
Aren't functions polymorphic in OCaml? They were polymorphic even in C++.Photodynamics
@SuzanCioc What type of polymorphism are you talking about here? As I said, in the context of OO programming, "polymorphism" usually refers to subtype polymorphism, i.e. the property that the system decides which function/method to call based on run-time type of the object. Free functions (or for that matter non-virtual methods) in C++ don't exhibit subtype polymorphism, that is the decision which function/method will be called happens entirely at compile time.Karisakarissa
If you're referring to the ability to overload functions (which is sometimes referred to as "ad-hoc polymorphism" - though personally I haven't heard that term used very often): Yes, you can overload functions in C++ (though note that here which function will be picked is determined entirely at compile time) and no, you can not overload functions in OCaml. One reason that OCaml doesn't allow overloading is that that feature does not play well with type inference.Karisakarissa
I think polymorphism is not about binding time. If I can use one form for different types then it is it, whenever binding occurs.Photodynamics
@SuzanCioc Subtype polymorphism is very much about binding time. If you're talking about a different type of polymorphism please say so explicitly.Karisakarissa
I didn't knew I was speaking about "different" type of polymorphism since when I was learned, it was C++'s function overloading :) Of course, virtual functions existed even then and later all functions is Java were made virtual. But even in Java functions remain "different"-style polymorphic. I.e. you can write several functions with same name but different parameter types. Isn't this possible in OCaml? So OCaml creators haven't made this polymorphism being runtime but just removed it, right?Photodynamics
@SuzanCioc As I said the term "polymorphism" in the context of OO languages (including C++) is most often used to refer to subtype polymorphism (i.e. virtual functions and such) - not to function overloading. Anyway, unlike subtype polymorphism, function overloading is not a defining characteristic of OO languages (nor in any way specific to them) and OCaml does not support it.Karisakarissa

© 2022 - 2024 — McMap. All rights reserved.