caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* "pointers" to methods
@ 2001-02-06 17:26 Mattias Waldau
  2001-02-07 22:27 ` John Prevost
  2001-02-07 23:23 ` Gerd Stolpmann
  0 siblings, 2 replies; 4+ messages in thread
From: Mattias Waldau @ 2001-02-06 17:26 UTC (permalink / raw)
  To: Caml-List

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.

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 ()

/mattias



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "pointers" to methods
  2001-02-06 17:26 "pointers" to methods Mattias Waldau
@ 2001-02-07 22:27 ` John Prevost
  2001-02-08 17:00   ` John Max Skaller
  2001-02-07 23:23 ` Gerd Stolpmann
  1 sibling, 1 reply; 4+ messages in thread
From: John Prevost @ 2001-02-07 22:27 UTC (permalink / raw)
  To: Mattias Waldau; +Cc: Caml-List

>>>>> "mw" == Mattias Waldau <mattias.waldau@tacton.se> 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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "pointers" to methods
  2001-02-06 17:26 "pointers" to methods Mattias Waldau
  2001-02-07 22:27 ` John Prevost
@ 2001-02-07 23:23 ` Gerd Stolpmann
  1 sibling, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2001-02-07 23:23 UTC (permalink / raw)
  To: Mattias Waldau, Caml-List

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                     
----------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "pointers" to methods
  2001-02-07 22:27 ` John Prevost
@ 2001-02-08 17:00   ` John Max Skaller
  0 siblings, 0 replies; 4+ messages in thread
From: John Max Skaller @ 2001-02-08 17:00 UTC (permalink / raw)
  To: John Prevost; +Cc: Mattias Waldau, Caml-List

John Prevost wrote:

>   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.

	Oh! This is a beautiful demonstration of the power of
the Ocaml language. Now I will add something: this construction
is MORE powerful than C++ pointers to members, which do not
form an algbraically complete system because they cannot be
composed. But the ocaml ones can be!

-- 
John (Max) Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-02-08 19:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-06 17:26 "pointers" to methods Mattias Waldau
2001-02-07 22:27 ` John Prevost
2001-02-08 17:00   ` John Max Skaller
2001-02-07 23:23 ` Gerd Stolpmann

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).