Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] extensible records again
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] extensible records again
On Sat, 20 Mar 2004, Michael Vanier wrote:

> So I tried this.  Aside from the pain of having to change data to 'a data in
> lots of places (which I can live with), I got bitten by the polymorphic
> reference limitation.  Specifically, I have a mutable stack of data values
> which became a mutable stack of 'a data values, but the function which
> creates the mutable stack is of type '_a data, so it doesn't type check.  I
> can't figure out any way around this.  Basically, my main data type _cannot_
> be parameterized.

Keep your code polymorphic: create your stack in the same compilation
unit as the full type instanciation.
Then you have to pass your stack as a parameter
to all functions that use it.

I guess this is more or less what you do:

(* a.ml *)
let stack =
  let st = Stack.create () in
  Stack.push (`Int 1) st;
  st
(* compiler complains about '_ *)


(* b.ml *)
let some_function x =
   match Stack.pop stack with
       `Int i -> print_int i
     | `Custom s -> print_string s
     | _ -> ()


Instead you should write:

(* a.ml *)
let init_stack () =
  let st = Stack.create () in
  Stack.push (`Int 1) st;
  st


(* b.ml *)
let some_function stack x =
  match Stack.pop stack with
      `Int i -> print_int i
    | `Custom s -> print_string s
    | _ -> print_float x

let _ =
  let stack = A.init_stack () in
  some_function stack 1.2


I hope it answers your question...

Martin


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners