Skip to content

Don't print hashCodes, print labels #808

Open
@remexre

Description

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions