From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 6BBB9BBC1 for ; Thu, 13 Mar 2008 10:45:14 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq0BAACV2EfAXQInh2dsb2JhbACQdAEBAQgKKZVx X-IronPort-AV: E=Sophos;i="4.25,493,1199660400"; d="scan'208";a="9380950" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 13 Mar 2008 10:45:10 +0100 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m2D9jAbv019782 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 13 Mar 2008 10:45:10 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkYGAACV2EfBL1AZYmdsb2JhbACQahUEBggIGZVx X-IronPort-AV: E=Sophos;i="4.25,493,1199660400"; d="scan'208";a="8338800" Received: from gw.exalead.com (HELO exalead.com) ([193.47.80.25]) by mail2-smtp-roc.national.inria.fr with ESMTP; 13 Mar 2008 10:44:58 +0100 Received: from [192.168.204.148] (madpc064.exalead.com [192.168.204.148]) (authenticated bits=0) by exalead.com (8.14.0/8.14.0) with ESMTP id m2D9iu4s018449; Thu, 13 Mar 2008 10:44:56 +0100 Message-ID: <47D8F798.30708@exalead.com> Date: Thu, 13 Mar 2008 10:44:56 +0100 From: Berke Durak User-Agent: Thunderbird 1.5.0.10 (X11/20070221) MIME-Version: 1.0 To: Ludovic Coquelle , Caml-list List Subject: Re: [Caml-list] invoke function from its name as string References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Miltered: at concorde with ID 47D8F7A6.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; berke:01 durak:01 berke:01 durak:01 dynlink:01 printf:01 printf:01 foo:01 dynlink:01 fprintf:01 byte:01 failwith:01 loadfile:01 cmo:01 argv:01 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