Module Import Bindings for Non-Main Classes

Per Bothner per@bothner.com
Wed Oct 26 19:01:00 GMT 2011


On 10/26/2011 11:45 AM, Taylor Venable wrote:
> Hi everybody, another question; this time about importing module fields.
> Suppose I have the following:
> ;; alpha.scm
> (module-name "com.example.Alpha")
> (define (test-a) (format #t "inside test-a~%"))
> (define (make-test-b) (lambda () (format #t "inside test-b~%")))
> (define test-b (make-test-b))
> (define test-c 42)

> ;; beta.scm
> (module-name "com.example.Beta")
> (import (only (com example Alpha) test-a test-b test-c))
> (define (test)
> (display test-a) (newline)
> (display test-b) (newline)
> (display test-c) (newline))
> (test)
>
> ;; gamma.scm
> (module-name "com.example.Gamma")
> (com.example.Beta:test)
>
> I'll compile these into classes.
>
> $ rm -rf classes/
> $ mkdir classes
> $ CLASSPATH='classes' kawa -d classes -C alpha.scm
> $ CLASSPATH='classes' kawa -d classes --main -C beta.scm
> $ CLASSPATH='classes' kawa -d classes --main -C gamma.scm
>
> Now I try to run Gamma and Beta, getting different results:
>
> $ java -cp lib/kawa-1.11.jar:classes 'com.example.Beta'
> #<procedure test-a>
> #<procedure alpha.scm:4>
> 42
> $ java -cp lib/kawa-1.11.jar:classes 'com.example.Gamma'
> #<procedure test-a>
> #!null
> #!null
>
> Why are test-b and test-c bound to the values they have in Alpha only
> when running Beta as the main class? How can I get the "right" bindings
> when running Gamma as the main class? I must be missing something
> obvious, but I can't figure it out. Thank you for any advice.

Quick answer: compile alpha.scm with --module-static-run or add
(module-static 'init-run) to alpha.scm.

Read 
http://www.gnu.org/software/kawa/Module-classes.html#How-a-module-becomes-a-class

Consider that evaluating the body of alpha.scm means evaluating various 
expression
and performing various initializations/assignments.  How should these 
effects be
performed?  Obviously, they have to be in some compiler-generated 
method, but which
method, and when and how does this method get invoked?  One issue is that if
we made --module-init-static run the default (i.e. bodies automatically 
evaluated
when the class is initialized) then you have no way of passing 
command-line arguments
- since the body would be evaluated before main starts.

This should possibly be a FAQ, but not sure how to phrase it FAQ-style.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/



More information about the Kawa mailing list