I would like to restructure my project from a flat file/module hierarchy into a more nested one. Here is what I have now:
. # Modules:
├── fruit_apple.ml # Fruit_apple
├── fruit_lemon.ml # Fruit_lemon
├── pie_apple.ml # Pie_apple
└── pie_lemon.ml # Pie_lemon
Here is what I want to get:
. # Modules:
├── fruit
│ ├── apple.ml # Fruit.Apple
│ └── lemon.ml # Fruit.Lemon
└── pie
├── apple.ml # Pie.Apple
└── lemon.ml # Pie.Lemon
OCaml has automatic mapping from file name to module name, but it doesn't seem to have one for directories and nested files.
I could have a pie.ml and fruit.ml file where I include the necessary submodules:
(* pie.ml *)
module Apple = struct
include Apple
end
module Lemon = struct
include Lemon
end
But I don't know how to resolve the ambiguity between pie/apple.ml and fruit/apple.ml.
I tried to study Core library, which has Core namespace with nested modules Core.Bool, Core.Bag and so on, but I couldn't find a core.ml file there which I would assume integrates all the submodules.
Since the introduction of module aliases in OCaml the preferred way to deal with this is to:
Develop modules in unambiguously identified modules with globally unique names.
If you wish, prepare a special module simulating a namespace, which might be nicer to the user than a flat collection of modules.
So, additionally to your current module definitions, you can add
(* Fruit.ml *)
module Apple = Fruit_Apple
module Lemon = Fruit_Lemon
(* Pie.ml *)
module Apple = Pie_Apple
module Lemon = Pie_Lemon
If appropriate, you should consider adding a global prefix identifying your library, so that other libraries can provide Fruit and Pie modules without competing with yours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With