[racket] Generics and gen:custom-write

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Mon Dec 2 15:16:43 EST 2013

`define/generic' binds the specified generic function to a new
identifier, to allow methods to call the generic function recursively.
In your case, your `write-proc' calls `super-write' which, after
dispatch, calls your `write-proc' again.

What you want would be some a bit like CLOS's `call-next-method', which
Racket's generics don't have.

I can't think of a way to do what you want, short of reimplementing the
behavior for `print' in your method.

The interface for `gen:custom-write' (which it inherited from
`prop:custom-write') is not great. IMO, it would be nicer to have
`write', `display' and `print' be separate methods (which would support
your use case), but I don't know how to retrofit that on top of the
existing custom-write interface provided by runtime system.

Vincent



At Mon, 2 Dec 2013 18:05:59 +0100,
Laurent wrote:
> 
> [1  <multipart/alternative (7bit)>]
> [1.1  <text/plain; ISO-8859-1 (7bit)>]
> Anyone has an answer to this?
> 
> Thanks,
> Laurent
> 
> 
> On Tue, Nov 12, 2013 at 1:58 PM, Laurent <laurent.orseau at gmail.com> wrote:
> 
> > 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
> >
> [1.2  <text/html; ISO-8859-1 (quoted-printable)>]
> 
> [2  <text/plain; us-ascii (7bit)>]
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.