caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Berke Durak <berke.durak@exalead.com>
To: Ludovic Coquelle <lcoquelle@gmail.com>,
	Caml-list List <caml-list@inria.fr>
Subject: Re: [Caml-list] invoke function from its name as string
Date: Thu, 13 Mar 2008 10:44:56 +0100	[thread overview]
Message-ID: <47D8F798.30708@exalead.com> (raw)
In-Reply-To: <d6c7b34d0803122107i392eabc6rea0570d8c4f05452@mail.gmail.com>

Ludovic Coquelle a écrit :
> First, hello everyone as it is my first message to this list ;)
> 
> I'm trying to solve this problem:
> I have a module name and function name in string variables,
> and I would like to call this function, like :
> let mod = "MyMod" in
> let fn = "my_fun" in
> what_come_here mod fn
> 
> Can this problem be solved?

There is a Turkish saying stating that "There is a solution to every
problem in a democracy"!

So here you go.

Note that since you're asking for a hackish operation, I felt authorized
to use a hackish solution, using Dynlink.

File alpha.ml:

let f () = Printf.printf "Alpha.f\n%!"

--

File foo.ml:

module A = Alpha

let _ = Dynlink.init ()

let call md fn =
   let oc = open_out "x.ml" in
   Printf.fprintf oc "let _ = %s.%s ()\n" md fn;
   close_out oc;
   if Sys.command "ocamlbuild x.byte" <> 0 then failwith "Duh";
   try
     Dynlink.add_interfaces ["alpha"] ["_build"];
     Dynlink.loadfile "_build/x.cmo"
   with
   | Dynlink.Error e ->
     Printf.printf "Too bad: %s\n%!" (Dynlink.error_message e)

let _ =
   call Sys.argv.(1) Sys.argv.(2)

--

% ocamlbuild.native -tags use_dynlink foo.byte
% ./foo.byte Alpha f
Alpha.f

-- 
Berke DURAK


      parent reply	other threads:[~2008-03-13  9:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-13  4:07 Ludovic Coquelle
2008-03-13  5:04 ` Ludovic Coquelle
2008-03-13  6:49   ` [Caml-list] " Andrej Bauer
2008-03-13  7:10     ` Ludovic Coquelle
2008-03-13 16:41       ` Andrej Bauer
2008-03-13 17:03         ` Andrew Gacek
2008-03-14  0:53           ` Ludovic Coquelle
2008-03-13  9:44 ` Berke Durak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47D8F798.30708@exalead.com \
    --to=berke.durak@exalead.com \
    --cc=caml-list@inria.fr \
    --cc=lcoquelle@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).