What is this syntax in the .ml files generated by ocamllex?

When I generate an .ml file with ocamllex it has a bunch of lines like:

# 21 "lib/myproj/example.ml"

(where the # is actually there in the code, I’m not copy and pasting from utop)

They look kind of like comments, but comments are always (* this is a comment *) right? VS Code doesn’t seem to treat them as comments either.

They almost look like a GOTO, with the line number and filename …but surely not?

Someone please enlighten me…

This is a line number directive: OCaml - The OCaml language .

This is used by code generator to synchronize locations in the generated file with locations in the source file.

2 Likes

Ahhh… thanks, makes sense :slight_smile:

Just to add a little bit: these #line directives are wonderful stuff. And the fact that they’re part of the OCaml syntax design, means that Xavier thought of the real world, and that preprocessors were necessary, back at the beginning of the language. Other languages have not made this choice: to wit, Java explicitly forswore preprocessing and hence does not have #line directives. [I have a vague memory that golang is in a similar boat, but maybe I misremember.]

This seems like a small, small thing. But like OCaml being first-and-foremost a batch-oriented language, and only afterwards having a toplevel, it’s actually huge from the point-of-view of a systems-builder.

3 Likes