Is there a systematic approach to preprocess .ml containing toplevel directives so that the preprocessed .ml output file can be compiled with ocaml compilers?
Toplevel directives like #directory would require some commandline arguments to be added to the ocaml compiler, but we are willing to take the complexity.
The background is that we have a few big .ml files that contain top directives. They are supposed to be used with #use "x.ml";; in OCaml REPL, but as the ml files are getting larger, running #use is now taking a significant time (around a minute). To make loading it faster, I am looking for a method to compile x.ml into .cmo/.cmi/.cma and load it.
Does using the floating attribute generate new .ml files that can be compiled with ocamlopt or ocamlc, or am I understanding wrong?
Custom attributes are ignored by the compiler. You can put there whatever you want.
You may also want to have a look at ocamlscript (or b0caml, but it’s unreleased and again one of these projects I had that was assuming we were going towards a simplification of the notion of library and needs to be reviewed now that this did not happen).
Testing in the TopLevel with .ml files that I also want to compile, I have 2 very simple and usual ways for doing that:
1/ Add a commented block of Toplevel directives in the source file and uncomment/evaluate/comment it when required.
It’s even just a shortcut away in your editor.
No compiler option. But source file is modified…
2/ For program foo.ml, put directives in foo.ml.directives.
But it requires first to write (or copy-past) and evaluate #directory “path/to/foo.ml.directives” and #use “directives_foo.ml” from the TopLevel…
Hence, I often handle a file foo.ml.directivenext tofoo.ml and simply copy-paste-evaluate its content in the Toplevel.
No compiler option. But source files directory is modified…
Something like this is not working and it also pollutes source file:
[@@@top.directory "my/dir"]
But it would be nice to add something similar that can be evaluated in the Toplevel while source files can be preprocessed to get rid of such directives.
I can also use a custom Toplevel with all the required env. But it’s not just launching the Toplevel and start playing with my source files.
It seems module Topdirs is not to be used in a program.
In 2026, is there a simple way to handle Toplevel directives in some format that can be preprocessed (by the compiler or so), so I can:
play with source files in the Toplevel, after a simple evaluation of directives
compile them at anytime without any change (even just commenting directives)
→ which options to ocamlc/ocamlopt ?
preprocess them to just filter out these “personal directives” before delivering them
→ which options to ocamlc/ocamlopt ?
If you put the files in a dune library, it will compile without modifications. Then you can load the library with dune utop. You don’t need toplevel directives for that, dune automatically does it for you.
I don’t need utop at all.
I already have a Toplevel environment setup file.
Why another setup file?… And another Toplevel?
And why using “dune, a build system designed for large programs”, which UI… has not yet been designed, for doing such a simple thing? Hence becoming dependent on some kind of beast.
Maybe you didn’t understand my question.
I’m not blocked at all.
I may be interested to improve my very lightweight procedure (i.e. evaluate a few expressions already written or generated) WITHOUT dealing with compiler options.
However, maybe I should consider compiler options to filter out all directives.