From: John Prevost <jmp@arsdigita.com>
To: "Mattias Waldau" <mattias.waldau@tacton.se>
Cc: "Caml-List" <caml-list@inria.fr>
Subject: Re: "pointers" to methods
Date: 07 Feb 2001 17:27:05 -0500 [thread overview]
Message-ID: <87n1by3z9i.fsf@elbereth.pgh.arsdigita.com> (raw)
In-Reply-To: <AAEBJHFJOIPMMIILCEPBKEHCCEAA.mattias.waldau@tacton.se>
>>>>> "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.
next prev parent reply other threads:[~2001-02-07 23:25 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 [this message]
2001-02-08 17:00 ` John Max Skaller
2001-02-07 23:23 ` Gerd Stolpmann
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=87n1by3z9i.fsf@elbereth.pgh.arsdigita.com \
--to=jmp@arsdigita.com \
--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