11

I'm reading an OCaml project recently and I want to put the source files in the utop so I can do some experiments.
Suppose I have two files amodule.ml, bmodule.ml.
bmodule.ml will use functions defined in amodule.ml, for example, bmodule use Amodule.anyfunction() where anyfunction() is defined in amodule.ml.
I want to put both of them in utop:

#directory "/directory contain amodule.ml and bmodule.ml"
#use "amodule.ml"
#use "bmodule.ml"

And this doesn't work because Amodule is a module name base on the amodule.ml file and the utop don't know these things, I think.
So how I can put these files in the utop without changing the file content?

Ashish Agarwal
  • 3,000
  • 16
  • 18
KUN
  • 527
  • 4
  • 18

1 Answers1

14

#use a.ml executes every statement in a.ml just as if you had typed those statements in the toplevel directly. Thus, you do not get a module A defined, so your other file cannot have things like A.foo. If you want module A, you must first byte compile a.ml, and then #load a.cmo.

Ashish Agarwal
  • 3,000
  • 16
  • 18
  • 7
    Newer versions of OCaml support `#use_mod` which should allow this without prior compilation. – hcarty Dec 15 '13 at 11:23
  • 11
    The directive mentioned by @hcarty is actually `#mod_use`, and it is very handy indeed. :) – Shon Jan 16 '17 at 23:34
  • 6
    `#mod_use` is exactly the piece of the puzzle I was missing. Thank you @ShonFeder! I can't believe Real World OCaml does not mention it even once. – Tobia Jul 30 '17 at 16:33
  • 1
    Anyone want some quick karma by turning `#mod_use` into a proper StackOverflow answer? – DomQ Jul 15 '20 at 08:27
  • Thanks @hcarty ! `#mod_use` is exactly what I've been looking for for hours, and it answers a very basic need. Unbelievable that it's not mentioned in popular introductions to OCaml. – user0 Sep 04 '21 at 15:20