Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:32:44+00:00 2026-06-01T12:32:44+00:00

I have a cyclic graph I created using dosync and ref-set . When I

  • 0

I have a cyclic graph I created using dosync and ref-set. When I pass this to println I get a java.lang.StackOverflowError as I would expect, because it’s effectively trying to print an infinitely-nested structure.

I found that if I do (str my-ref) it creates something that looks like vertex@23f7d873 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 (println my-graph) have it print the ref as some type of custom text (possibly involving str), and the other non-ref stuff normally.

Currently I have a custom print function that prints each element of the struct on its own and completely skips printing the ref. (It turns out that looking at vertex@23f7d873 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 swank.core/break debug thingy.

One detail is the ref is actually a value in a defstruct that also contains some other stuff which I am trying to print normally.

So I’m wondering what path I should go down. I see these options:

  1. Figure out extend-type and apply the CharSequence protocol to my defstructed structure so that when it comes across a ref it works properly. This still requires a field-by-field inspection of the struct and a special case when it comes to the ref, but at least it localizes the problem to the struct and not to anything that contains the struct.
  2. Figure out how to override the CharSequence protocol when it comes across a ref. 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.
  3. Figure out how to do something with toString which I believe is called at some level when I do println. I’m most ignorant about this option. Pretty ignorant about the other ones too, but I’ve been reading Joy of Clojure and I’m all inspired now.

Likewise this solution should apply to print and pprint and anything else that would normally barf when trying to print a cyclic ref. What strategy should I employ?

thanks a lot for any input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:32:46+00:00Added an answer on June 1, 2026 at 12:32 pm

    What you want to do is create a new namespace and define your own print functions that models the way clojure prints objects, and defaults to clojure’s methods.

    In detail:

      Create an ns excluding pr-str and print-method.
      Create a multimethod print-method (copy exactly what is in clojure.core)
      Create a default method that simply delegates to clojure.core/print-method
      Create a method for clojure.lang.Ref that doesn’t recursively print everything

    As a bonus, if you are using clojure 1.4.0, you can use tagged literals so that reading in the output is also possible. You would need to override the *data-readers* map for a custom tag and a function that returns a ref object. You’d also need to override the read-string behavior to ensure binding is called for *data-readers*.

    I’ve provided an example for java.io.File

     (ns my.print
       (:refer-clojure :exclude [pr-str read-string print-method]))
    
     (defmulti print-method (fn [x writer]
             (class x)))
    
     (defmethod print-method :default [o ^java.io.Writer w]
           (clojure.core/print-method o w))
    
     (defmethod print-method java.io.File [o ^java.io.Writer w]
           (.write w "#myprint/file \"")
           (.write w (str o))
           (.write w "\""))
    
     (defn pr-str [obj]
       (let [s (java.io.StringWriter.)]
         (print-method obj s)
         (str s)))
    
     (defonce reader-map
       (ref {'myprint/file (fn [arg]
                   (java.io.File. arg))}))
    
     (defmacro defdata-reader [sym args & body]
       `(dosync
         (alter reader-map assoc '~sym (fn ~args ~@body))))
    
     (defn read-string [s]
       (binding [*data-readers* @reader-map]
         (clojure.core/read-string s)))
    

    Now you can call like so (println (my.print/pr-str circular-data-structure))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set which is a large unweighted cyclic graph The cycles
I have a directed cyclic graph. Some edges are FIXED and may not be
Have you ever created or encountered a self modifying code in Java? If yes,
I have a directed cyclic graph with values at edges but no values at
I try to have a treeview that browse potentially cyclic hierarchical data. This means
Assume we have a path in an undirected cyclic weighted graph. Assuming we have
i have to Find the longest path in a directed cyclic graph from a
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
I have a graph of components and relations between them. User start navigation with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.