Scripted main in OCaml?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 16:21:00

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.

$ 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!