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 AAA12752 for caml-red; Thu, 8 Feb 2001 00:25:51 +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 XAA27380 for ; Wed, 7 Feb 2001 23:27:50 +0100 (MET) Received: from elbereth.pgh.arsdigita.com ([63.124.128.66]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f17MRnH13679 for ; Wed, 7 Feb 2001 23:27:50 +0100 (MET) Received: by elbereth.pgh.arsdigita.com (Postfix, from userid 1000) id 80F1036C6A; Wed, 7 Feb 2001 17:27:05 -0500 (EST) To: "Mattias Waldau" Cc: "Caml-List" Subject: Re: "pointers" to methods References: From: John Prevost Date: 07 Feb 2001 17:27:05 -0500 In-Reply-To: Message-ID: <87n1by3z9i.fsf@elbereth.pgh.arsdigita.com> User-Agent: Gnus/5.090001 (Oort Gnus v0.01) Emacs/21.0.91 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: weis@pauillac.inria.fr >>>>> "mw" == Mattias Waldau writes: mw> In the following code I would like to apply the method inc or mw> dec to the object obj. However, what is the syntax I should mw> use? At all comments below the compilation fails. ------------------------------------------------------------------------ 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 () ------------------------------------------------------------------------ Try this let main () = let call_method = if Random.int 2 = 0 then fun x -> x #inc else fun x -> x #dec in let obj = new class1 in call_method obj 2; obj #get () You can't reference a method name explicitly, but you can create a function that calls that method. This is fairly equivalent. If you wanted the function to be more like a non-function value, just make it opaque and add a "call_method" function. John.