title: How to override println behavior for reference types
body: <p>I have a cyclic graph I created using <code>dosync</code> and <code>ref-set</code>.  When I pass this to <code>println</code> I get a <code>java.lang.StackOverflowError</code> as I would expect, because it's effectively trying to print an infinitely-nested structure.  </p>
  
  <p>I found that if I do <code>(str my-ref)</code> it creates something that looks like <code>vertex@23f7d873</code> and doesn't actually try to traverse the structure and print everything out, so this solves the problem in the immediate sense, but only helps when I'm very careful about what I'm printing to the screen.  I'd like to be able to call <code>(println my-graph)</code> have it print the <code>ref</code> as some type of custom text (possibly involving <code>str</code>), and the other non-ref stuff normally.</p>
  
  <p>Currently I have a custom print function that prints each element of the struct on its own and completely skips printing the <code>ref</code>. (It turns out that looking at <code>vertex@23f7d873</code> is not actually very useful).  This is awkward to use and hinders greatly doing casual inspection of stuff at the REPL and also prevents Emacs inspector from looking at stuff while I'm in a <code>swank.core/break</code> debug thingy.</p>
  
  <p>One detail is the <code>ref</code> is actually a value in a <code>defstruct</code> that also contains some other stuff which I am trying to print normally.</p>
  
  <p>So I'm wondering what path I should go down.  I see these options:</p>
  
  <ol>
  <li>Figure out <code>extend-type</code> and apply the <code>CharSequence</code> protocol to my <code>defstruct</code>ed structure so that when it comes across a <code>ref</code> it works properly.  This still requires a field-by-field inspection of the struct and a special case when it comes to the <code>ref</code>, but at least it localizes the problem to the struct and not to anything that contains the struct.</li>
  <li>Figure out how to override the <code>CharSequence</code> protocol when it comes across a <code>ref</code>.  This allows even more localized behavior and allows me to view a cyclic ref at the REPL even when it's not inside a struct.  This is my preferred option.</li>
  <li>Figure out how to do something with <code>toString</code> which I believe is called at some level when I do <code>println</code>.  I'm most ignorant about this option.  Pretty ignorant about the other ones too, but I've been reading <code>Joy of Clojure</code> and I'm all inspired now.</li>
  </ol>
  
  <p>Likewise this solution should apply to <code>print</code> and <code>pprint</code> and anything else that would normally barf when trying to print a cyclic ref.  What strategy should I employ?</p>
  
  <p>thanks a lot for any input.</p>