difference in struct printing from default #:transparent versus make-constructor-style-printer

29 views
Skip to first unread message

Jeremy Siek

unread,
Sep 2, 2020, 4:07:07 PM9/2/20
to Racket Users
I'm seeing some bad behavior from make-constructor-style-printer when
there are lists mixed in with the structs. It seems to switch from print mode to
write mode when going under a list. Whereas the default printer for transparent structs gets this right. The following program demonstrates the difference/problem.
Any thoughts on how to get the make-constructor-style-printer to behave
properly?

#lang racket
(require racket/struct)

(struct Int1 (value) #:transparent)
(struct Prim1 (op arg*) #:transparent)

(struct Int2 (value) #:transparent
  #:methods gen:custom-write
  [(define write-proc
     (make-constructor-style-printer
      (lambda (obj) 'Int2)
      (lambda (obj) (list (Int2-value obj)))))])
(struct Prim2 (op arg*) #:transparent
  #:methods gen:custom-write
  [(define write-proc
     (make-constructor-style-printer
      (lambda (obj) 'Prim2)
      (lambda (obj) (list (Prim2-op obj) (Prim2-arg* obj)))))])

(define p1 (Prim1 '+ (list (Int1 1) (Int1 2))))
(print p1)(newline)

(define p2 (Prim2 '+ (list (Int2 1) (Int2 2))))
(print p2)(newline)

The output is:

(Prim1 '+ (list (Int1 1) (Int1 2)))
(Prim2 '+ '(#<Int2: 1> #<Int2: 2>))

-Jeremy

Ryan Culpepper

unread,
Sep 3, 2020, 5:47:50 AM9/3/20
to Jeremy Siek, Racket Users
The Racket printer uses a separate property to determine whether a struct is quotable. If you add 

  #:property prop:custom-print-quotable 'never

to the declarations of Int2 and Prim2, your program should produce the output you expect. I'll add a note to the docs for make-constructor-style-printer.

Ryan


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/bdeb1052-ea25-4dc0-8292-a13d15bf7870n%40googlegroups.com.

Jeremy Siek

unread,
Sep 3, 2020, 8:48:30 AM9/3/20
to Racket Users
Thanks Ryan!
Reply all
Reply to author
Forward
0 new messages