From: Hans Ole Rafaelsen <hans@simula.no>
To: caml-list@inria.fr
Subject: Polymorphic function in reference cell
Date: Wed, 18 Nov 2009 15:33:46 +0100 [thread overview]
Message-ID: <4B0405CA.80704@simula.no> (raw)
Hi,
I have a module that have several functions that take a polymorphic
function as part of their arguments (Foo.f1 and Foo.f2 in the example).
module Foo = struct
let f1 f =
f 1
let f2 f =
f 1.0
let f1_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) ))
let set_f1_ref f =
f1_ref := f
let f2_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) ))
let set_f2_ref f =
f2_ref := f
let use_f1 () =
!f1_ref 1
let use_f2 () =
!f2_ref 1.0
let internal_f v =
v
let internal_f1 () =
internal_f 1
let internal_f2 () =
internal_f 1.0
end
let f v =
v
let a = Foo.f1 f
let b = Foo.f2 f
let () = Foo.set_f1_ref f
let () = Foo.set_f2_ref f
let c = Foo.use_f1 ()
let d = Foo.use_f2 ()
In my code I don't want to pass this function around to to all functions
where Foo.f1 is called. For this reason I would like to store this
function that Foo.f1 needs within Foo. (The function is created outside
the module and bound with values generated outside the module.) However
you can not store polymorphic functions in references. So I have to make
one reference for each concrete type it is used for. What I really would
like is use it as if it was defined within Foo, like internal_f and used
like the internal_f1 and internal_f2.
Is it possible to only store one reference to this kind of function
within a module, or do I really have to write a reference for each usage
of the function? If I have to create a reference for each case, do
anyone have some trick to avoid code repetition?
Regards,
Hans Ole
next reply other threads:[~2009-11-18 14:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-18 14:33 Hans Ole Rafaelsen [this message]
2009-11-18 14:58 ` [Caml-list] " Marc de Falco
2009-11-18 16:59 ` Hans Ole Rafaelsen
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=4B0405CA.80704@simula.no \
--to=hans@simula.no \
--cc=caml-list@inria.fr \
/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