Previous: Module system, Up: Concepts [Contents][Index]
By default, Gauche reads toplevel Scheme forms one at a time, compile it immediately to intermediate form and execute it on the VM. As long as you use Gauche interactively, it looks like an interpreter. (There’s an experimental ahead-of-time compiler as well. See HOWTO-precompile.txt if you want to give a try.)
The fact that we have separate compilation/execution phase, even interleaved, may lead a subtle surprise if you think Gauche as an interpreter. Here’s a few points to keep in mind:
load
is a procedure in Gauche, therefore evaluated at run time.
If the loaded program defines a macro, which is available for the compiler
after the toplevel form containing load
is evaluated. So, suppose
foo.scm
defines a macro foo
, and you use the macro
like this:
;; in “foo.scm” (define-syntax foo (syntax-rules () ((_ arg) (quote arg)))) ;; in your program (begin (load "foo") (foo (1 2 3))) ⇒ error, bad procedure: ‘1’ (load "foo") (foo (1 2 3)) ⇒ '(1 2 3)
The (begin (load ...))
form fails, because the compiler
doesn’t know foo
is a special form at the compilation time
and compiles (1 2 3)
as if it is a normal procedure call.
The latter example works, however, since the execution
of the toplevel form (load "foo")
is done before
(foo (1 2 3))
is compiled.
To avoid this kind of subtleties, use require
or use
to load a program fragments. Those are recognized by the compiler.
On the other hand, since require
and use
is recognized
by the compiler, the specified file is loaded even if the form
is in the conditional expression. If you really need to load
a file on certain condition, use load
or do dispatch in macro
(e.g. cond-expand
form (see Feature conditional).)
Previous: Module system, Up: Concepts [Contents][Index]