[racket-dev] Custom write that pretty-prints and works well with quote?

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sat Jul 7 14:40:49 EDT 2012

I've got an array structure like so:

(struct: (A) strict-array ([shape : (Listof Index)]
                            [data : (Vectorof A)])

Say I have this value:

     (strict-array '(2 2) #(1 2 3 4))

I want it to print like this at the REPL:

     #<strict-array '(2 2) '[[1 2] [3 4]]>

If there's not enough room, I want this:

       '(2 2)
       '((1 2) (3 4))>

If it happens to be in a list, I want this:

     (list #<strict-array '(2 2) '[[1 2] [3 4]]>)

and definitely NOT '(#<strict-array '(2 2) '[[1 2] [3 4]]>) because the 
inner quotes are confusing.

How do I do that with prop:custom-write? The only way I've found so far 
to get nicely formatted output is to write/display/print a list, but 
then the quotes get messed up. I haven't found a way to keep the REPL 
from quoting lists of structs, except to go with the default printer, 
which flattens arrays, which makes them hard to read.

This is frustrating.

Neil ⊥

Posted on the dev mailing list.