[racket] Generics and modules

From: Konrad Hinsen (konrad.hinsen at fastmail.net)
Date: Tue Aug 27 11:37:21 EDT 2013

Asumu Takikawa writes:

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

Great, thanks! I had seen define/generic but its documentation made me
believe that its use is more restricted. But it works fine in my
demonstration code.

Unfortunately, I did not manage to fix the real code that motivated my
question using the same trick. The difference is that in the real code,
the method name shadows not a local function, but a field accessor in
the very structure that I am defining the methods for.

Here's an updated version of my demo code:

-- foo.rkt ------------------------------------------------
#lang racket

(provide (all-defined-out))

(require racket/generic)

(define-generics bar
  [foo-bar-baz bar])
-----------------------------------------------------------

-- foo-bar.rkt --------------------------------------------
#lang racket

(provide (all-defined-out))

(require (prefix-in foo: "foo.rkt")
         (only-in racket/generic define/generic))

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

Then, in a REPL:

    (require (prefix-in foo: "foo.rkt")
             "foo-bar.rkt")
    (foo:foo-bar-baz (foo-bar 42))

This hangs in the method foo-bar-baz, so it looks like my
freshly defined gen-foo-bar-baz still refers to the method
being defined.

Is there a workaround for that case as well?

Thanks,
  Konrad.

Posted on the users mailing list.