caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Polymorphic variant to string?
@ 2005-11-16 18:44 Chris Campbell
  2005-11-16 21:29 ` [Caml-list] " Jacques Garrigue
  2005-11-17 13:44 ` Richard Jones
  0 siblings, 2 replies; 3+ messages in thread
From: Chris Campbell @ 2005-11-16 18:44 UTC (permalink / raw)
  To: caml-list

Hi,

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.

The polymorphic variant type resides in a library and is used by a
module in my program.  I need to check the values returned match which
is easy, except when the code tested is erroneous and I want to see
what's going on.

Any ideas?  Suggestions?


Cheers,
Chris


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Polymorphic variant to string?
  2005-11-16 18:44 Polymorphic variant to string? Chris Campbell
@ 2005-11-16 21:29 ` Jacques Garrigue
  2005-11-17 13:44 ` Richard Jones
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2005-11-16 21:29 UTC (permalink / raw)
  To: cyberdanx; +Cc: caml-list

From: Chris Campbell <cyberdanx@gmail.com>

> 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.

The internal representation of a polymorphic variant tag is a hash by
a given function (you can find it in typing/btype.ml)
So, if you have the list of all possible variant tags, and their hash
values, you just have to do a reverse lookup.
Note that in theory, you need a list by type, as some independent
types might map different tags to the same hash value, but in practice
this should not be a problem: the probability of conflict is low
enough.
And of course this only apply to variant tags, not to argument values.

Jacques Garrigue


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Polymorphic variant to string?
  2005-11-16 18:44 Polymorphic variant to string? Chris Campbell
  2005-11-16 21:29 ` [Caml-list] " Jacques Garrigue
@ 2005-11-17 13:44 ` Richard Jones
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Jones @ 2005-11-17 13:44 UTC (permalink / raw)
  To: Chris Campbell; +Cc: caml-list

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 test.ml
print_endline (Std.dump [ `Foo; `Bar; `Baz 123.0 ])
rich@oirase:/tmp$ ocamlfind ocamlc -package extlib test.ml -linkpkg -o test
rich@oirase:/tmp$ ./test
[3505894; 3303859; (3303867, 123.)]

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-11-17 13:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-16 18:44 Polymorphic variant to string? Chris Campbell
2005-11-16 21:29 ` [Caml-list] " Jacques Garrigue
2005-11-17 13:44 ` Richard Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).