melt-umn / silver

An attribute grammar-based programming language for composable language extensions
http://melt.cs.umn.edu/silver/
GNU Lesser General Public License v3.0
58 stars 7 forks source link

Don't print hashCodes, print labels #808

Open remexre opened 11 months ago

remexre commented 11 months ago

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: