Question

I need a way to create GraphViz node names from CLOS objects in such a way that each object gets its own node, and if I alter my objects and re-create the GraphViz visualization, I get the same node names for objects that stay (referentially) the same.

If I just try printing my object, I get something that is almost good (since I never override PRINT-OBJECT for my class):

CL-USER> (format nil "~A" *g*)
"#<GREF {1002D22C81}>"

Is there a way to get just that 1002D22C81 part as a string? I could then create GraphViz node names like N1002D22C81 from that.

Or should I just process the result of (format nil "~A" obj) as a string by grabbing the part between {}?

Was it helpful?

Solution

The hex number is the object address. It can change after a garbage collection. Your implementation may provide a function to get it directly, but I don't think you should use it.

What you might consider doing is adding a name slot to your objects and automatically initializing them using, say, gensym.

If you want to keep track of all your objects, you might even intern the names in a special package and set their symbol-value to the objects (beware that this will make the objects uncollectable by the GC until you unintern their names, or unset their symbol-value, or delete the aforementioned special package).

PS. You can get the object address even if you override print-object - just pass :identity t to print-unreadable-object.

PPS. I am sure you know that (format nil "~A" x) is the same as (princ-to-string x).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top