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


On Sun, Jul 28, 2019 at 6:12 AM Jesse Wang <hello....@gmail.com> wrote:

> Thanks, it works!
>
> On Sunday, July 28, 2019 at 5:54:06 PM UTC+8, Sorawee Porncharoenwase
> wrote:
>>
>> 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))
>>
>>
>> On Sun, Jul 28, 2019 at 2:45 AM Jesse Wang <hell...@gmail.com> wrote:
>>
>>> 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.
>>>
>>> --
>>> 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...@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
>>> <https://groups.google.com/d/msgid/racket-users/213fb232-d7fe-49f9-90d3-981c8662a0c5%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> 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/12cd77d3-76e5-4e6a-a81f-6bb61071e628%40googlegroups.com
> <https://groups.google.com/d/msgid/racket-users/12cd77d3-76e5-4e6a-a81f-6bb61071e628%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAH3z3gZQz80JRtCmdpYFaOvAOt7sVH_BO0o-ACa39jz%3D8_HmOQ%40mail.gmail.com.

Reply via email to