caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Fwd: Re: [Caml-list] string_of_polymorphic
@ 2005-01-03 11:20 Jon Harrop
  0 siblings, 0 replies; only message in thread
From: Jon Harrop @ 2005-01-03 11:20 UTC (permalink / raw)
  To: caml-list


> >Why do you want to do this?
>
> Logging purposes, Error handling and so on.
> I have a function that accept some polymorphic constructors as input
> type, and I want to log what is arrived before possibly without writing
> everytime a pattern matching ad-hoc, or raising an exception, I'd like
> to attach the constructor that raised the exception.

If the program is running in the top-level then you may be able to use the
relevant installed pretty printer. I've no idea how though. :-)

> >I think the run-time representation of a polymorphic variants' value is a
> > hash of its name and, therefore, cannot be mapped back onto a string in
> > general. There may be something else you can do specifically for the
> > top-level but I can't think what...
>
> I don't know.
> Polymorphic constructors survive to the marshalling-unmarshalling.
> In the marshalled string, there is a representation of their name.
> That's why I thought there was a method to get their names into a function.
> I think that if Obj is strongly not recommended, analizing a marshalled
> string has to be avoided, so I was looking for other ways.

I believe that the name of a polymorphic variant, as a string, is not in the
marshalled string. Instead, the name is in the type, which is not conveyed by
marshalling. I think the marshalled string will actually contain a fixed-size
hash of the string name.

In theory, two polymorphic variant names which produce the same hash should
 be indistinguishable at run-time. Producing them will be tricky though.

A consequence of this is the fixed-size (1-word) of a marshalled polymorphic
variant:

# Marshal.to_string `dkdfbgkadjfbgljbfgljkabhvlkjblvbalvbvlabalbvaslbk [];;
- : string =
"\132\149¦¾\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\000\0
02í\014âí" # Marshal.to_string `abcd [];;
- : string =
"\132\149¦¾\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\000\0
02ÀheÂ"

Cheers,
Jon.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-01-03 11:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-03 11:20 Fwd: Re: [Caml-list] string_of_polymorphic Jon Harrop

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