How can I prettyprint a reference value so that I get some idea from looking at the output whether two such values are the same or not?
I have a data type that is littered with various references, and the default pretty-printer only gives me strings with "ref" through them. If I have "ref 1" in one place and "ref 1" in another, I have no idea whether or not these are the same references.
If I want to handle this situation in pure SML, it seems as if I'd have to maintain a global list of seen references and look each reference up by doing a linear search through the list. This is pretty awful. Even if there was a Ref.compare function so that I could avoid linear search, the table would still need to be global so that successive pretty-prints would continue to make sense with respect to each other.
In Moscow ML there is an Obj.magic function that will turn anything into anything, and in particular seems happy to turn a ref into an integer. Is there something comparable in Poly/ML?
(I browsed the poly sources in a very cursory fashion and discovered RunCall.unsafeCast, but this either gave me a segmentation fault, or seemed to be looking at the values at the other ends of the references when I applied it to them.)
Michael.
Michael Norrish wrote:
How can I prettyprint a reference value so that I get some idea from looking at the output whether two such values are the same or not?
I would suggest using PolyML.showSize. This displays the actual contents of an arbitrary data structure in hex and includes the addresses of the cells. You need to be aware that any garbage collection between two calls to showSize may result in the addresses changing.
RunCall.unsafeCast is just that: extremely unsafe, and assumes that you know the representations of the types before and after the cast are compatible. Casting a ref to an int will cause the address of the ref cell to be treated as the address of a piece of memory containing the long precision representation of an integer and will either give nonsense or could segfault.
David