I have a module in OCaml that is parameterized by another module, which represents a data structure (H = Hashtable, M = Map, L = LossyMap). I would now like to let this data structure be selected via the command line.
The way I create the main processing module is:
module HashSampler = MakeSampler(HashtableMatrix)
module MapSampler = MakeSampler(MapMatrix)
etc.
Unfortunately, the code that multiplexes between these is ugly:
match representation with
| "Hashtable" ->
let matrix = HashSampler.create () in
HashSampler.process_file matrix file
| "Map" ->
let matrix = MapSampler.create () in
MapSampler.process_file matrix file
Is there a better way of doing this that somehow prevents code duplication?
You can use first class modules. Here's some example code that shows one possibility.