Functions
Polymorphic functions accept arguments of parametric types, that is, a larger category of inputs, as long as the function does not assume a particular type for the argument (so called uniform polymorphism).
Perhaps the most trivial example is the identity function.
For any given type t, there is a canonical function from type t to
type t: it takes an argument and returns it immediately. For
instance, we can write the identity function for int as follows:
However, if we would want to use the same function on a different
type, such as nat, we will need to write a new definition:
If we read carefully, we see that there is almost no difference
between id_int and id_nat: it is just the type that changes, but
for the rest, the body of the function remains the same.
Thanks to parametric polymorphism, we can write a single function declaration that works for both cases.
Here T is a type variable which can be generalised. If we have more
than one type parameter, we list them like so:
We can now call the function id with arguments of different
types:
During compilation, LIGO will monomorphise the polymorphic functions into specific instances, resulting in Michelson code that does not contain polymorphic function declarations anymore.