[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.3 Module initialization
Initializing a module means evaluating, at runtime, its top level forms (global bindings are top level forms).
When a module, module1, imports a module, module2, module2 is initialized before module1. Modules are initialized only once, nothing being done if a module already met during initialization is met again. Library modules are initialized before user modules and imported modules are initialized in the same order as they appear in import clauses.
Here is a first example with two modules. First the module foo
:
;; module foo
(module foo
(main main)
(import (bar "bar.scm")))
(define (main argv)
(print "argv: " argv))
(print "foo")
Then the module bar
;; module bar
(module bar)
(print "bar")
These can be compiled into the executable a.out with:
$ bigloo -c foo.scm $ bigloo -c bar.scm $ bigloo foo.o bar.o
Execution of a.out produces:
$ a.out -| bar foo argv: (a.out)
The explanation is:
- module
foo
contains the program entry point so this is where initialization begins. - because
foo
imports modulebar
,bar
must be initialized beforefoo
. This explains why the wordbar
is printed before anything else. - module initialization for
foo
is completed beforemain
is called. This explains why wordfoo
is printed beforemain
is entered.
Let’s consider another example with 3 modules:
;; module1
(module module1
(main main)
(import (module2 "module2.scm")))
(define (main argv)
(print "argv: " argv))
(print "module1")
The second module:
;; module2
(module module2
(import (module3 "module3.scm")))
(print "module2")
The third module:
;; module3
(module module3
(import (module1 "module1.scm")))
(print "module3")
Compile with:
$ bigloo module1.scm -c $ bigloo module2.scm -c $ bigloo module3.scm -c $ bigloo module1.o module2.o module3.o
Execution produces:
$ a.out -| module3 module2 module1 argv: (a.out)
The order of module initialization can be explicitly specified using with
and use
clauses.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.