[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.6 Compilation

Gauche is a Scheme interpreter, in the sense that it reads a Scheme form at a time and evaluates it. Actually, Gauche compiles every toplevel form into an intermediate form before executing.

Built-in sytanxes and macros are recognized and expanded at the compilation time. Some built-in procedures are expanded in-line as far as the compiler can see the global binding is not altered at the time the form is compiled.

This raises a few problems you should care.

load is done at run time.
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.

require is done at compile time
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 (i.e. at compile time).

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html