As we have seen, modules are important in OCaml and ReasonML, helping to organize code into units with specified interfaces. In addition to that, we are going to see now that they can serve for building generic code using what are called Functors.
Generic code with functors
What is a functor?
A functor is a function whose parameters are modules and whose result is a module.
Functors allow us to extend existing modules with new functionality, without requiring a lot of repetitive code for the different types we have.
The syntax of functors looks as follows:
module F = (M1: I1, ···): ResultI => {
...
};
With these characteristics, note the following:
- The F functor has as parameters one or more M1 modules...