I am trying to understand for myself, which form of polymorhism does OCaml language have.
I was provided by an example
let id x = x
Isn’t this example equivalent to C++ template function
template<class A> A id(A x) { return x; }
If so then my question is: are there any other forms of polymorphism in OCaml? This notion is called “generic algorithm” in the world of imperative languages, not “polymorphism”.
There are basically three language features that are sometimes called polymorphism:
As you already discovered, OCaml has parametric polymorphism. It also has subtype polymorphism. It does not have ad-hoc polymorphism.
Since in your title you’ve asked for examples, here’s an example of subtype polymorphism in OCaml:
This will print
4.