Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional module aliasing

Tags:

module

ocaml

For a class project I am writing a program that evaluates the performance of different implementations of the same abstract data structure. Since I am using identical code to test each of them, I would like to be able to set a module alias depending on user input and just run that module through the testing code.

In other words, I want something like:

let module M = 
  if model = "tree" then TreeModel else
  if model = "hash" then HashModel else
  ListModel
in ...

Is there a way I can make this work, or am I going about this all wrong?

like image 758
Aniket Schneider Avatar asked Mar 12 '26 02:03

Aniket Schneider


1 Answers

There are no conditionals on the module level, but you can use first-class modules for this:

let m = match model with
  | "tree" -> (module TreeModel : MODEL)
  | "hash" -> (module HashModel : MODEL)
  | "list" -> (module ListModel : MODEL)
in let module M = (val m : MODEL)
in ...
like image 180
Andreas Rossberg Avatar answered Mar 15 '26 05:03

Andreas Rossberg