[racket] Generics and gen:custom-write

From: Laurent (laurent.orseau at gmail.com)
Date: Tue Nov 12 07:58:46 EST 2013

I'm trying to implement a custom writer for a struct with generics, but I
think I'm misunderstanding something.

What I want to do is specialize only the behavior for `write' and
`display', but leave the behavior for `print' untouched. So I thought that
is what define/generic was for, but the following loops indefinitely:

#lang racket
(require racket/generic)

(struct A (x y)
  #:transparent
  #:methods gen:custom-write
  [(define/generic super-write write-proc)
   (define (write-proc a port mode)
     (case mode
       [(#t) (write (list (A-x a) (A-y a)) port)]
       [(#f) (display (list (A-x a) (A-y a)) port)]
       [else (super-write a port mode)]))])

(define a (A 1 'b))
(displayln a) ; ok
(write a)(newline) ; ok
(print a)(newline) ; infinite loop

What's the correct way to do that then?

Thanks,
Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131112/a595ef17/attachment.html>

Posted on the users mailing list.