Calling functions by a string variable?

Is it possible in Ocaml to call a function by giving a string variable.
If there is no existing libraries, how can I do this by myself?

Thanks!

You mean like

let func = "do_something" in
func ()

?

The above itself is not possible, but you can achieve something similar by putting functions in a map-like (dictionary) structure and indexing them by their names, e.g.

let () =
  let funcs = [ "do_something", fun () -> print_endline "Doing something" ] in
  try (List.assoc "do_something" funcs) ()
  with Not_found -> print_endline "Could not find function"
1 Like