Browse thread
Fwd: Re: [Caml-list] string_of_polymorphic
- Jon Harrop
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jon Harrop <jon@j...> |
| Subject: | Fwd: Re: [Caml-list] string_of_polymorphic |
> >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.