-
Book Overview & Buying
-
Table Of Contents
Learn Bosque Programming
By :
Parametric polymorphism is a way in which programming languages have implemented polymorphism while keeping full-static safety. This happens through generics, so that a function does not depend on the type of its parameters.
By definition, parametric polymorphism allows us to have one implementation supporting many different types. Let’s see an example.
The following snippet shows us a function that receives two integers and returns the sum of them:
sum(a: Int, b: Int): Int {
return a + b;
}
But what if we also want to support the sum of two values of the Float64 type? We would have to implement a new method designed for Float64 values, as shown in the following snippet:
sum(a: Float64, b: Float64): Float64 {
return a + b;
}
But writing an additional method with the same functionality is not the smartest thing to do. We could apply parametric polymorphism and write a single function that works for both Float64 and Int...