[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.
> [...]

Hello,

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^
		(a))
	
	(define-signature b^
		(b))
	
	(provide
	 (all-defined-out))
	-----END "ab-sig.rkt"-----
	
	-----BEGIN "a.rkt"-----
	#lang racket
	(require
	 "ab-sig.rkt")
	
	(define-unit a@
		(import b^)
		(export a^)
		(define a 1)
		(printf "a@ sees b = ~s~%" b))
	
	(provide
	 (all-defined-out))
	-----END "a.rkt"-----
	
	-----BEGIN "b.rkt"-----
	#lang racket
	(require
	 "ab-sig.rkt")
	
	(define-unit b@
		(import a^)
		(export b^)
		(define b 2)
		(printf "b@ sees a = ~s~%" a))
	
	(provide
	 (all-defined-out))
	-----END "b.rkt"-----
	
	-----BEGIN "ab.rkt"-----
	#lang racket
	(require
	 "ab-sig.rkt" "a.rkt" "b.rkt")
	
	(define-values/invoke-unit/infer
		(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 :-)

Ciao,
Thomas


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


Posted on the users mailing list.