Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't print hashCodes, print labels #808

Open
remexre opened this issue Sep 27, 2023 · 0 comments
Open

Don't print hashCodes, print labels #808

remexre opened this issue Sep 27, 2023 · 0 comments

Comments

@remexre
Copy link
Member

remexre commented Sep 27, 2023

When we're printing things, the hashCode is pretty much only useful for noticing if something is circular. However, in medium-to-large objects, it won't "pop out at you" if an object is circular; you have to read and remember all the hashCodes!

Common Lisp's syntax has a notion of labeled objects to enable parsing and printing circular objects (CLHS 2.4.8.15, CLHS 2.4.8.16). Essentially:

CL-USER> (setf *print-circle* t)
T
CL-USER> (let ((x (list 1 2 3 4 5))) (setf (cdr (last x)) x))
#1=(1 2 3 4 5 . #1#)
CL-USER> (let* ((x (list 1 2 3 4 5)) (y (list 6 7 x 9 x))) (setf (cdr (last x)) y))
#1=(6 7 #2=(1 2 3 4 5 . #1#) 9 #2#)

This is a lot more readable than addresses/hashCodes! Importantly:

  • we only print the "extra thing" when there's actually a circular term
  • the extra thing "pops out at you"
  • the labels are easy to remember; just small integers
  • you can see how much circularity there is at a glance -- am I seeing numbers like 3 or like 130
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant