24

In SML's repl, you can just type use whatever.sml and load all things inside that .sml into repl.

How can I do that in OCaml?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
  • fml, why are there so many REPL questions? I want to load an .ml file from another in an actual program not the f'ing REPL lol. I don't understand why people even use a REPL...please educate me. – Alexander Mills Jan 06 '19 at 03:02

1 Answers1

29

You have #use directive for that purpose:

#use "file-name";;

Read, compile and execute source phrases from the given file. This is textual inclusion: phrases are processed just as if they were typed on standard input. The reading of the file stops at the first error encountered.

For example (as per @gasche's suggestion):

# #use "whatever.ml";;

Here is a complete list of OCaml directives.

pad
  • 41,040
  • 7
  • 92
  • 166
  • 13
    Note that the `#` of `#use` must really be typed by the user as an additional character, this is not a reference to the beginning prompt. The whole line will therefore look like `# #use "file.ml";;`. Users are frequently confused about this. – gasche Feb 07 '13 at 16:27
  • @gasche: Thanks, I added your example for clarification. – pad Feb 07 '13 at 19:23
  • Link to ocaml directives is broken – NPN328 Sep 25 '22 at 12:12