Mostly to get around circular dependency. i.e. I define
action type in jsoo_todomvc.ml which is the topmost/root module, i.e. not other modules depend on it but it itself depends on other modules such as
Footer, Newtodo and so on. That type (
action) is used in other components/modules such as new_todo.ml and footer.ml which makes it circular, i.e. module
Jsoo_todomvc depends on
New_todomvc depends on
Jsoo_todomvc which is not allowed in ocaml - as it shouldn’t. Using polymorphic variant breaks this circular dependency. Perhaps this wasn’t the intended purpose of polymorphic variants but it is quite effective in this scenario.
You can get around circular modular dependency via recursive modules but in this instance it felt bit heavy since using recursive module you have to restructure your source code in a certain way,i.e define your dependent modules in the same file.
Another alternative to get around this circular dependency issue is to define the type in
std.ml so you can use normal variant. However, I went ahead with polymorphic variant in this instance as they are quite flexible as compared to normal variants.