Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Polymorphic variant to string?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-17 (13:24)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Polymorphic variant to string?
On Wed, Nov 16, 2005 at 06:44:34PM +0000, Chris Campbell wrote:
> Is there a general method for converting polymorphic variants to
> strings?  The method using camlp4 assumes the source is compiled with
> it, which is not the case.  Basically I want to print a list of
> variants if a unit tests fail, so we can see exactly the contents of
> the list at that point.

You might want to try Std.dump in extlib.  It only prints out the
hash, but you can precalculate those given a list of variant names.

rich@oirase:/tmp$ cat
print_endline (Std.dump [ `Foo; `Bar; `Baz 123.0 ])
rich@oirase:/tmp$ ocamlfind ocamlc -package extlib -linkpkg -o test
rich@oirase:/tmp$ ./test
[3505894; 3303859; (3303867, 123.)]


Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology -
Team Notepad - intranets and extranets for business -