I have an OCaml project using dune
Following advice in basic tutorials I have a dir structure like:
bin/
cli.ml
dune
lib/
dune
...
<various>.ml
The number of files in my lib
dir is growing and I would like to have another level of namespacing.
I want sub dirs like:
lib/
utils/
dune
...
<various>.ml
some_other_domain/
dune
...
<various>.ml
dune
...
<various>.ml
And I want to be able to open them like Lib.Utils.Whatever
I assume this must be possible?
I tried making a dune
file under lib/utils
like:
(library
(name utils)
(libraries ...))
...but open Lib.Utils.Whatever
doesn't seem to work.
I found the subdir
stanza ...but if I add that to lib/dune
and define utils
as a subdir library
then I don't get the namespacing... I have to open Utils
rather than open Lib.Utils
It's actually a bit strange to call them "nested libraries" since you want to call them with
Lib.Utils.Whatever
.Utils
, here, is a sub-module ofLib
. Here's what I was able to do if it can help you:With
bin/cli.ml
bin/dune
lib/dune
(If you include your modules like this, you'll have to use them with these exact names, another way of gaining control is to add the following file and remove the
(modules suba subb)
line:lib/lib.ml
(to summarise:
dune
file is containing(modules suba subb)
(modules ...)
stanza or the compiler won't be able to use themlib.ml
file where each module you want to export should be included withmodule MyName = AModule
(and only the ones you want to export)lib.ml
file, the compiler will use them if needed )sub{a|b}/{a|b}.ml
suba.ml
andsubb.ml
are used as sub-modules oflib
and can be used withLib.Suba.a()
as you can see incli.ml
Notice that this forbids you from giving the exact same name to two files since the directories will all be flatten in the parent directory so you can't have something like:
because
(include_subdirs unqualified)
will make it look likeand
dune
won't be able to know whichlib.ml
file to use.[EDIT] If you want one directory per library you just have to remove the
dune
file at the root oflib
and create one for each subdirectories:The only changes are:
bin/cli.ml
bin/dune
lib/dune
has been removedlib/sub{a|b}/dune
And in that case, multiple files inside different directories can have the same name.