[racket] Oh, load! Where art thou?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Jul 23 22:20:54 EDT 2010

Robby pointed you to chapter 15 in the Guide, which is what I was going
to do, but here are a few extra answers:

At Fri, 23 Jul 2010 19:58:52 -0600, Richard Cleis wrote:
> Given a file of "some-scheme.rxt":
> *********************
> ;#lang racket
> (define a-var 'a-val)
> *********************
> and a program in a definitions window:
> *********************
> #lang racket
> (define (f) (load "some-scheme.rkt"))
> ;(f)
> *********************
> ... Entering (f) in the interactions window defines a-var.

Right --- evaluating `(f)' has the side-effect of defining `a-var' in
the current namespace, such as the namespace is the one that is active
for the REPL in DrRacket.

> If the comment is removed from ;#lang racket, a-var is not defined.  Where did 
> it go?

If the file starts `#lang racket', then it declares a module. The
`a-var' definition is inside the module. Also, `load' simply loads the
module declaration without actually instantiating the module.

> If the comment is removed from ;(f), an 'unbound identifier' error is 
> triggered during the Run. 

The `load' function performs a run-time action. The expansion/compilation 
of the program containing `(f)' doesn't look at "some-scheme.rkt",
because that doesn't happen later when/if `(f)' is evaluated.

> How can I explain where (load) evaluates it's contents?

Chapter 15 covers that one.

> Does (load) have a future in racket?

As Robby says, `load' has no place as a substitute for `require'; it's
just a variant of `eval' that pulls expressions from a file.

There are times when you want to script a sequence of `eval's, `load's,
and others side-effects on a namespace; `#lang racket/load' can help
with that. Chapter 15 explains more, including some reasons why
`racket/load' is usually a bad idea.

Posted on the users mailing list.