Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripted main in OCaml?

How can I emulate this Python idiom in OCaml?

if __name__=="__main__":
   main()

See RosettaCode for examples in other programming languages.

like image 778
mcandre Avatar asked Sep 29 '11 22:09

mcandre


1 Answers

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.

like image 89
Gilles 'SO- stop being evil' Avatar answered Sep 28 '22 16:09

Gilles 'SO- stop being evil'