At Thu, 31 Aug 2017 11:31:33 +0200, Konrad Hinsen wrote:
> On 25/08/2017 17:03, Konrad Hinsen wrote:
> 
> > That adds the missing piece - thanks a lot! I had seriously 
> > underestimated the complexity of module definitions in Racket.
> 
> A followup mostly for the benefit of those who find this thread in the 
> archives one day.
> 
> Importing a module this way works perfectly, but its utility for testing 
> is very limited. The problem I run into is illustrated by the following 
> symptom:
> 
> raco test: (submod "lang-test.rkt" test)
> document-contexts: contract violation;
>   given value instantiates a different structure type with the same name
>    expected: document?
>    given: (document (hash "test-context"  ...
> 
> 
> My code defines a structure called "document", which apparently exists 
> in two copies in memory. One is used by the module lang-test.rkt that 
> contains the tests, and the other one is used by the module that was 
> created from the text string. As a consequence, it is impossible to 
> perform any operation on the date from that module.

If you'd like the test namespace and the test-driving module to share
the instance of the module that defines `document` (so that they'll
agree on the data structure), you can use `namespace-attach-module` to
attach the test-driving module's instance to a newly created namespace.

For example, with

 ;; a.rkt:
 #lang racket/base
 (provide (struct-out a))
 (struct a (x))

 ;; b.rkt:
 #lang racket/base
 (require "a.rkt")
 (parameterize ([current-namespace (make-base-namespace)])
   (eval `(require "a.rkt"))
   (a-x (eval `(a 1))))

then you get an error

 a-x: contract violation;
  given value instantiates a different structure type with the same name
   expected: a?
   given: #<a>

but attaching the original instance of "a.rkt" avoids the error:

 ;; b.rkt, revised:
 #lang racket/base
 (require "a.rkt")
 (define-namespace-anchor here)
 (parameterize ([current-namespace (make-base-namespace)])
   (namespace-attach-module (namespace-anchor->namespace here)
                            '"a.rkt")
   (eval `(require "a.rkt"))
   (a-x (eval `(a 1))))

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to