Syntax Error #use Ocaml

For some reason, everytime I call the method #use, which opens all the functions from one file, there is always a Syntax Error at the beginning. The code works fine, but it annoys me, because the type inference decides to stop working (for some reason) because there is a syntax error.

Here is the problem:

How can I fix it?

# directives are not part of the language.

Then, ocamlc will see a syntax error too.

The best thing with a multi files project is to have a dune and dune-project files which make it easy to compile all file together.

Then in a.ml, simply type:

let () = B.f ()

And dune will detect the dependence and add b.ml into the files to be linked.

I agree dune is better, but ocamlc actually does not error when you have #use directives. Try it and you’ll see :slight_smile:

Hey, thanks for the fast replies. Is there any useful website, tutorial or other topic in this discuss that I can use to learn Dune?

Or, if you’re kind, you could also help me directly. I have currently five files named like you saw in the image.

The main file is the logic.ml and uses all of the other files.

The parser uses the lexer and the expression (which is where I defined the grammar).

The expression uses the utilities.

All of the files are inside the same in my Mac.

With that in mind, how can I start using Dune?

Hi, you can get started with opam and dune here: Your First OCaml Program · OCaml Documentation

Once these tools are set up, you will just need a project structured like this:

proj/
  dune
  dune-project
  expression.ml
  lexer.ml
  logic.ml
  parser.ml
  utils.ml

The dune-project file will contain:

(lang dune 3.6)

The dune file will contain:

(executable
  (name logic)
)

Then you can run your program with: dune exec ./logic.exe.

Dune will automatically link all your modules together in the right order.

Let’s try…

$ocamlc a.ml
File "a.ml", line 1, characters 0-1:
1 | #use "b.ml"
    ^
Error: Syntax error

Ah, sorry you’re right, it works only with the ocaml bytecode interpreter which understands these directives. It doesn’t work with the compiler.