2

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.

Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
  • core is packed with `-pack` option, so that every module automatically enveloped in `Core` module. What you're looking is `std.ml` – ivg Apr 11 '15 at 17:07
  • possible duplicate of [How to make a multi-level module hierarchy with (or without) Oasis](http://stackoverflow.com/questions/28722704/how-to-make-a-multi-level-module-hierarchy-with-or-without-oasis) – ivg Apr 11 '15 at 17:11

1 Answers1

2

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.

Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57