[racket] Generics and modules

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Tue Aug 27 09:10:21 EDT 2013

On 2013-08-27 14:31:11 +0200, Konrad Hinsen wrote:
> -----------------------------------------------------------
> (struct foo-bar (quux)
>         #:methods foo:gen:bar
>         [(define (baz x) (baz (foo-bar-quux x)))])
> -----------------------------------------------------------
> But now the call to baz inside the method becomes a recursive call to
> the method, rather than a call to the function baz defined in the
> surrounding module. In fact, that's why I used (prefix-in ...) in the
> first place.

This is what the `define/generic` form is for. You can then re-write
your struct definition as

   (struct foo-bar (quux)
           #:methods foo:gen:bar
           [(define/generic gen-baz baz)
            (define (baz x) (gen-baz (foo-bar-quux x)))])

and it should work.

> I tried to get around this with a local binding:
> -----------------------------------------------------------
> (let ([temp baz])
>   (struct foo-bar (quux)
>           #:methods foo:gen:bar
>           [(define (baz x) (temp (foo-bar-quux x)))]))
> -----------------------------------------------------------

The error you're getting means that you need an expression to run in the
`let` block and not just definitions. You also probably don't want to
put the struct type definition in the `let` block because that makes the
struct type local to that block.


Posted on the users mailing list.