Why custom-display of printable<%> interface is called twice?

23 views
Skip to first unread message

Jesse Wang

unread,
Jul 28, 2019, 5:45:30 AM7/28/19
to Racket Users
Hi, I created a simple class to test the printable<%> interface:
(define my-class%
  (class* object% (printable<%>)
    (super-new)

    (define/public (custom-display out)
      (displayln "custom-display"))
    (define/public (custom-write out)
      (writeln "custom-write"))
    (define/public (custom-print out depth)
      (print "custom-print\n" out))))

But when I try to display an object of this class, the custom-display function is called twice, why?
>(define obj (new my-class%))
>(display obj)
custom-display
custom-display

And When I write the object, custom-display is also called first:
> (write obj)
custom-display
"custom-write"

With print:
> (print obj)
custom-display
custom-display
"custom-print\n"


I think I must have missed something here, and I can't find anything about this in the documentation of printable<%>, please help.
Thanks.

Sorawee Porncharoenwase

unread,
Jul 28, 2019, 5:54:06 AM7/28/19
to Jesse Wang, Racket Users

I’m not sure why it’s like that, but I know what’s the problem and how to fix it.

custom-display and custom-write provide the output port to you. You are supposed to write to the port.

    (define/public (custom-display out)
      (displayln "custom-display" out))
    (define/public (custom-write out)
      (writeln "custom-write" out))

--
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/213fb232-d7fe-49f9-90d3-981c8662a0c5%40googlegroups.com.

Jesse Wang

unread,
Jul 28, 2019, 6:12:11 AM7/28/19
to Racket Users
Thanks, it works!
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.

Philip McGrath

unread,
Jul 28, 2019, 11:12:46 PM7/28/19
to Jesse Wang, Racket Users
Someone had a very similar question about `printable<%>`, including both the confusion about multiple calls and the bug of failing to use the `out` port, on Stack Overflow recently: https://stackoverflow.com/questions/55975708/racket-equivalents-of-pythons-repr-and-str/56759937#56759937

Since these have apparently been points of confusion for multiple people, I'm wondering if there's something that could be improved in the documentation to help.

-Philip


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/12cd77d3-76e5-4e6a-a81f-6bb61071e628%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages