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.mlbin/dunelib/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:
dunefile is containing(modules suba subb)(modules ...)stanza or the compiler won't be able to use themlib.mlfile where each module you want to export should be included withmodule MyName = AModule(and only the ones you want to export)lib.mlfile, the compiler will use them if needed )sub{a|b}/{a|b}.mlsuba.mlandsubb.mlare used as sub-modules ofliband can be used withLib.Suba.a()as you can see incli.mlNotice 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
dunewon't be able to know whichlib.mlfile to use.[EDIT] If you want one directory per library you just have to remove the
dunefile at the root ofliband create one for each subdirectories:The only changes are:
bin/cli.mlbin/dunelib/dunehas been removedlib/sub{a|b}/duneAnd in that case, multiple files inside different directories can have the same name.