How to organise an OCaml project?

I think this is generally less uniform than it is in some other communities, so most of these have a sort of either-or answer:

  • One common structure is the lib, bin, test layout. This is promoted by Real World OCaml and is also what you get if you use say dune init project. That said, I also have projects that just have a src/ folder that contains both “library”/implementation code and an executable stub, and a test/ folder for any tests.

  • This may not be what you’re asking, but the only real navigational naming convention I can think of is that many modules will have a “core type” just named t. The standard library has this with e.g. the Result module defining Result.t

  • I handle dependencies by having each project manage its own opam switch. If I was just learning I would probably use local switches, but by now I’m used to just differently-named global switches. I believe the major alternative here is using something like opam-monorepo

  • For testing I mainly use a mix of inline/expect testing and cram-style testing. Both of these are run with dune runtest

3 Likes

I’d say the alternative is rather dune package management. opam-monorepo is an interesting concept but unless you need some of the very specific features of it, Dune package management is the better option.

2 Likes