From: Gerd Stolpmann <gerd@gerd-stolpmann.de>
To: "Mattias Waldau" <mattias.waldau@tacton.se>,
"Caml-List" <caml-list@inria.fr>
Subject: Re: "pointers" to methods
Date: Thu, 8 Feb 2001 00:23:55 +0100 [thread overview]
Message-ID: <01020800290309.02644@ice> (raw)
In-Reply-To: <AAEBJHFJOIPMMIILCEPBKEHCCEAA.mattias.waldau@tacton.se>
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
----------------------------------------------------------------------------
prev parent reply other threads:[~2001-02-08 18:11 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-02-06 17:26 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 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=01020800290309.02644@ice \
--to=gerd@gerd-stolpmann.de \
--cc=caml-list@inria.fr \
--cc=mattias.waldau@tacton.se \
/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