Re: Question about GADT
"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]> Thu, 3 Dec 2015 23:42:35 +0100
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAPFanBFP8iF=z0BuGntDwRCvXstEO7uvvpkdL8PavhJDG5R8Vg@mail.gmail.com> |
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. > > > ------------------------------------ > Posted by: Stephane Legrand <[email protected]> > ------------------------------------ > > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr > Attachments are banned and you're asked to be polite, avoid flames etc. > ------------------------------------ > > Yahoo Groups Links > > >