Printing an object as a unique string, possibly using its address
I need a way to generate GraphViz node names from CLOS objects in such a way that each object gets its own node, and if I modify my objects and re-create the GraphViz visualization, I get the same node object names that remain (essentially) the same.
If I just try to print my object, I get something almost good (as I never override PRINT-OBJECT
for my class):
CL-USER> (format nil "~A" *g*)
"#<GREF {1002D22C81}>"
Is there a way to get just this part 1002D22C81
as a string? Then I could create GraphViz node names like N1002D22C81
.
Or do you just need to treat the result (format nil "~A" obj)
as a string, capturing the part in between {}
?
source to share
The hexadecimal number is the address of the object. This may change after garbage collection. Your implementation can provide a function to get it directly, but I don't think you should be using it.
What you can do is to add the slot name
to your objects and automatically initialize them using, say gensym
.
If you want to keep track of all your objects, you can even intern
names in a special package and set them symbol-value
to objects ( Beware ) that the GC will make the GC unreproducible you your names or cancel them or remove the aforementioned special package ). unintern
symbol-value
PS. You can get the address of an object even if you override print-object
- just go :identity t
to print-unreadable-object
.
PPS. I'm sure you know what is the (format nil "~A" x)
same as (princ-to-string x)
.
source to share