Search code examples
ocamlutopdune

What is the namespace in utop for definitions in .ml file listed in dune's (executable ...) rule?


What is the namespace of the test0 definition in dune utop . environment, given the following ocaml code with dune build definition:

~/_ocaml_/n01$ cat bin/dune
(executable
 (public_name n01)
 (name main))

~/_ocaml_/n01$ cat bin/main.ml 
let test0 = "World!"
let () = print_endline  ("Hello " ^ test0)

When started the code produces expected result:

~/_ocaml_/n01$ dune build
~/_ocaml_/n01$ dune exec n01
Hello World!     

Solution

  • I believe that your issue is that you cannot access the test0 variable after launching dune utop ..

    If I am not mistaken, your issue stems from the fact that the dune top <path> command loads libraries and not executable. Thus in your setup, the N01 module is not part of the utop environment. The idiomatic solution is to split your source files into a library and a small executable that only contains glue code.