How can I load a ml file in toplevel of OCaml, just like `use mine.sml` in SML/NJ?

13,907

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.

Share:
13,907

Related videos on Youtube

Jackson Tale
Author by

Jackson Tale

OCaml Driver for MongoDB OCaml encoder/decoder for BSON Many things about OCaml

Updated on June 06, 2022

Comments

  • Jackson Tale
    Jackson Tale almost 2 years

    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?

    • Alexander Mills
      Alexander Mills over 5 years
      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.
  • gasche
    gasche about 11 years
    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.
  • pad
    pad about 11 years
    @gasche: Thanks, I added your example for clarification.