Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Polymorphism and the "for" loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Polymorphism and the "for" loop
I've got a problem which is sort of related to this discussion.  I
want to store a function which has type unit -> 'a (which is to say, I
want to store a function which could return any type) in a hash table.

Later, I want to call this function, ignoring the return type.

A diluted example, with my notes added:

  # let f = ref None;;
  val f : '_a option ref = {contents = None}
  # let store_f f' = f := Some f';;
  val store_f : '_a -> unit = <fun>

  (I want store_f to have type (unit -> 'a) -> unit.)

  # let do_f () =
      match !f with
      | None -> print_endline "no function"
      | Some f -> ignore (f ());;
  val do_f : unit -> unit = <fun>
  # store_f;;
  - : (unit -> '_a) -> unit = <fun>

  (Almost ...)

  # store_f (fun () -> print_endline "this is the stored function");;
  - : unit = ()
  # do_f ();;
  this is the stored function
  - : unit = ()
  # store_f;;
  - : (unit -> unit) -> unit = <fun>

I also tried replacing the definition of store_f with:

  # let store_f (f' : unit -> 'a) = f := Some f';;
  val store_f : (unit -> '_a) -> unit = <fun>

Is this possible?

Rich.

-- 
Richard Jones.  http://www.annexia.org/  http://www.j-london.com/
>>>   http://www.team-notepad.com/ - collaboration tools for teams   <<<
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://youunlimited.co.uk/ - Personal improvement courses