Scripted main in OCaml?

527 Views Asked by At

How can I emulate this Python idiom in OCaml?

if __name__=="__main__":
   main()

See RosettaCode for examples in other programming languages.

2

There are 2 best solutions below

4
On BEST ANSWER
$ ocamlc -o scriptedmain -linkall str.cma scriptedmain.ml
$ ./scriptedmain
Main: The meaning of life is 42
$ ocamlc -o test -linkall str.cma scriptedmain.ml test.ml
$ ./test
Test: The meaning of life is 42

scriptedmain.ml:

let meaning_of_life : int = 42

let main () = print_endline ("Main: The meaning of life is " ^ string_of_int meaning_of_life)

let _ =
    let program = Sys.argv.(0)
    and re = Str.regexp "scriptedmain" in
        try let _ = Str.search_forward re program 0 in
            main ()
        with Not_found -> ()

test.ml:

let main () = print_endline ("Test: The meaning of life is " ^ string_of_int Scriptedmain.meaning_of_life)

let _ =
    let program = Sys.argv.(0)
    and re = Str.regexp "test" in
        try let _ = Str.search_forward re program 0 in
            main ()
        with Not_found -> ()

Posted on RosettaCode.

5
On

There is no notion of main module in Ocaml. All the modules in a program are equal. So you can't directly translate this Python idiom.

The usual way in Ocaml is to have a separate file containing the call to main, as well as other stuff like command line parsing that only make sense in a standalone executable. Don't include that source file when linking your code as a library.

There is a way to get at the name of the module, but it's rather hackish, as it is intended for debugging only. It violates the usual assumption that you can rename a module without changing its behavior. If you rely on it, other programmers reading your code will curse you. This method is provided for entertainment purposes only and should not be used in real life.

let name_of_this_compilation_unit = 
  try assert false with Assert_failure (filename, _, _) -> filename

You can compare the name of the compilation unit with Sys.executable_name or Sys.argv.(0). Note that this is not really the same thing as the Python idiom, which does not rely on the toplevel script having a particular name.