From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA05276 for caml-red; Thu, 8 Feb 2001 19:11:11 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA15984 for ; Thu, 8 Feb 2001 00:29:35 +0100 (MET) Received: from moutvdom00.kundenserver.de (moutvdom00.kundenserver.de [195.20.224.149]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f17NTYH14328 for ; Thu, 8 Feb 2001 00:29:34 +0100 (MET) Received: from [195.20.224.209] (helo=mrvdom02.schlund.de) by moutvdom00.kundenserver.de with esmtp (Exim 2.12 #2) id 14Qe1g-0003sN-00; Thu, 8 Feb 2001 00:29:28 +0100 Received: from drms-3e364b7e.pool.mediaways.net ([62.54.75.126] helo=ice.gerd-stolpmann.de) by mrvdom02.schlund.de with esmtp (Exim 2.12 #2) id 14Qe24-0003j1-00; Thu, 8 Feb 2001 00:29:52 +0100 Received: from localhost (localhost [[UNIX: localhost]]) by ice.gerd-stolpmann.de (8.9.3/8.9.3) id AAA21786; Thu, 8 Feb 2001 00:29:03 +0100 From: Gerd Stolpmann Reply-To: gerd@gerd-stolpmann.de Organization: privat To: "Mattias Waldau" , "Caml-List" Subject: Re: "pointers" to methods Date: Thu, 8 Feb 2001 00:23:55 +0100 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain References: In-Reply-To: MIME-Version: 1.0 Message-Id: <01020800290309.02644@ice> Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr On Tue, 06 Feb 2001, Mattias Waldau wrote: >In the following code I would like to apply the method inc or dec to the >object obj. However, what is the syntax I should use? At all comments below >the compilation fails. You can use partially evaluated method calls as pointers to methods. However, this works only for methods with at least one argument: let main() = let obj = new class1 in let method_to_call = if Random.int 2 = 0 then obj # inc else obj # dec in method_to_call 2; obj # get() ;; For methods without arguments, one has to explicitly create a function representing the pointer. If inc and dec had no argument: let main() = let obj = new class1 in let method_to_call = if Random.int 2 = 0 then (fun () -> obj # inc) else (fun () -> obj # dec) in method_to_call (); obj # get() ;; >class class1 = > object (self) > val mutable x = 1 > method inc step = x <- x+step > method dec step = x <- x+step > method get () = x > end > >let main () = > let method_to_call = > if Random.int 2 = 0 then > inc (* pointer to inc-method in class1 *) > else > dec (* pointer to dec-method in class1 *) > in > let obj = new class1 in > obj#method_to_call 2; (* apply pointer to method in class1 *) > obj#get () > Gerd -- ---------------------------------------------------------------------------- Gerd Stolpmann Telefon: +49 6151 997705 (privat) Viktoriastr. 100 64293 Darmstadt EMail: gerd@gerd-stolpmann.de Germany ----------------------------------------------------------------------------