This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Module of loaded files


Chris Dean wrote:

Per Bothner <per@bothner.com> wrote:

You need a (require <M>) in F.scm.

Some clarification. Note that require may behave differently for compiled and "interpreted" code, but we try to keep things consistent.


require at compile-time looks for the named class, and adds the names exported by the class (derived from public field names) to the lexical scope. At run-time in compiled code it will run the "body" of the imported class. In interpreted mode (i.e. at the command line), it will just add the exported names to the current environment.

So if (in the first message) Driver.scm is compiled, then when it does (require <M>) the func is visible, but it is not re-exported in the global run-time environment. When F.scm is loaded, it will look for func in the global environment, and not find it, hence the error.

If I compile a different version of Driver.scm with "java kawa.repl
--main -C Driver.scm" then func is accessible.  Bug? feature? confusion
on my part?

==> F.scm <==
(func)

==> Driver.scm <==
(module-name <Driver>)

(define (func)
  'in-driver)

(load "F.scm")

Here for some reason func gets placed in the global run-time environment, so the call in F.scm can see it. I can't off-hand tell if this is a bug or feature.


Generally, if you're compiling and using modules, you should avoid load. The static bindings of modules provide both better name space management, and better performance. Using load is like using reflection (though not as slow, of course): The performance is worse, and you lose some of the help the compiler can give you in catching errors.

I've thought about adding an option to emit a warning or error at compile-time is a name is undefined, forcing all names to be lexically bound using modules. This would be good for catching typos and other errors at compile times, and would also be good for people who want the best performance. (This is in the same category as invoke warning when it can't find a method.)
--
--Per Bothner
per@bothner.com http://per.bothner.com/




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]