[racket] use variable(in separated file) mutually ?

From: Thomas Chust (chust at web.de)
Date: Sat Nov 13 05:53:58 EST 2010

2010/11/13 김태윤 <kty1104 at gmail.com>:
> [...]
> I was told by mailing list that read the racket guide unit section
> but sorry I still don't understand.
> [...]


you have to use units here because modules do not allow mutually recursive
dependencies. An example for your situation may look like this:

	-----BEGIN "ab-sig.rkt"-----
	#lang racket
	(define-signature a^
	(define-signature b^
	-----END "ab-sig.rkt"-----
	-----BEGIN "a.rkt"-----
	#lang racket
	(define-unit a@
		(import b^)
		(export a^)
		(define a 1)
		(printf "a@ sees b = ~s~%" b))
	-----END "a.rkt"-----
	-----BEGIN "b.rkt"-----
	#lang racket
	(define-unit b@
		(import a^)
		(export b^)
		(define b 2)
		(printf "b@ sees a = ~s~%" a))
	-----END "b.rkt"-----
	-----BEGIN "ab.rkt"-----
	#lang racket
	 "ab-sig.rkt" "a.rkt" "b.rkt")
		(export a^ b^)
		(link a@ b@))
	(printf "ab.rkt sees a = ~s~%" a)
	(printf "ab.rkt sees b = ~s~%" b)
	-----END "ab.rkt"-----

As you can see, the module dependencies are cycle free but the unit dependency
between a@ and b@ is mutual.

Note that, even though a@ and b@ can see each other's variables exported through
the signatures a^ and b^ respectively, the units still have to be initialized
in some order and hence one of the printf calls in a@ or b@ will show an
undefined value when "ab.rkt" is run.

I hope this helps :-)


When C++ is your hammer, every problem looks like your thumb.

Posted on the users mailing list.