Re: Question about GADT
"Stephane Legrand [email protected] [ocaml_beginners]" <[email protected]> Fri, 4 Dec 2015 00:15:22 +0100
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
I'm trying to unwrap the data built with "data_int" or "data_string". Ie i'm trying to retrieve the original value "v" used in "data_int" or "data_string" but only by using the "to_ocaml_value" function. I don't even know if it's possible? On 03.12.2015 23:42, Gabriel Scherer [email protected] [ocaml_beginners] wrote: > The program has a type error, as (v) in the last sentence could be > either (Int n) or (String s). The compiler is right to complain. What > are you trying to do? > > On Thu, Dec 3, 2015 at 11:34 PM, Stephane Legrand [email protected] > [ocaml_beginners] <[email protected]> wrote: > > Hello, > > > > I'm trying to run the code below. The compiler gives me an error for the > > "print_endline x" line: "This expression has type ex#0 but an expression > > was expected of type string". Is there a way to fix this error? If this > > kind of thing is doable with GADT? Thank you. > > > > type _ data = > > Int : int -> int data > > | String : string -> string data > > > > type t = > > Wrap : _ data -> t > > > > let data_int v = Wrap(Int v) > > let data_string v = Wrap(String v) > > > > let to_ocaml_value : type a. a data -> a = function > > | Int v -> v > > | String v -> v > > > > let test = data_string "test" > > > > let () = > > match test with > > | Wrap v -> > > let x = to_ocaml_value v in > > print_endline x > > > > > > Regards, > > Stéphane. > > > >