I am `gen:custom-write` and `make-constructor-style-printer` on an object 
graph that may contain cycles. Within my custom write procedure, I use 
`parameterize` to register an object into dictionary and later in the 
writing process I can check that dictionary, detect the cycle, and do more 
custom write logic.

But I have noticed that sometimes that my `parameterize` has gone out of 
scope by the time the printer gets deeper into the object graph. I was able 
to work around this issue by using another port, then copying the result to 
the real port like this:

(define-syntax-rule (write-now proc x port mode)
  (let* ([alt-port (open-output-string)]
         [_ (proc x alt-port mode)]
         [str (get-output-string alt-port)])
    (write-string str port)))

This is better than just doing `(proc x port mode)` because it always 
prints the string that I expect. However, it breaks pretty printing. I've 
noticed that the built-in pretty printing logic will sometimes hit the same 
leaf object multiple times, as if it is testing to see how wide the result 
is and backing up and trying again. Perhaps it can also delay evaluation? 
That would explain why my `parameterize` has ended.

Is there a way to use `parameterize` within custom write logic and keep 
pretty printing working? Is there a better approach I should take?



Additional Information
===
https://github.com/default-kramer/morsel/blob/master/morsel-lib/private/essence/base-query-printer.rkt#L22
The `write-now` macro, as written, is "correct but unpretty". I can change 
it to "incorrect but pretty" as follows:

(define-syntax-rule (write-now proc x port mode)
  (proc x port mode))


https://github.com/default-kramer/morsel/blob/master/morsel-lib/private/essence/from.rkt#L373
This is a test that fails in "incorrect but pretty" mode. Rackunit reports:

location:   from.rkt:373:2
actual:
  "(from #0=a/0 \"A\" (attach #1=b/1 \"B\" (attach #2=c/2 \"C\") #3=(list 
#0# #1# #2#)))"
expected:
  "(from a/0 \"A\" (attach b/1 \"B\" (attach c/2 \"C\") (list a/0 b/1 
c/2)))"


Here are two screenshots, one pretty and one unpretty:

[image: pretty.png]

[image: unpretty.png]

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/6369f1c7-a8a5-470e-af9f-ce1c133469ca%40googlegroups.com.

Reply via email to