Scope of an Object

An important distinction for authors to keep in mind is the difference between compile-time and run-time. The state of the lexicon when a script is first compiled (the currently defined objects, commands and programs) will in general not be the same as it will be if the same script is run or compiled at a later time, or in a different Document. An object or construct is global if it is installed in the lexicon. Otherwise, it is local. Usually, the lexicon of a Document, when it starts up, is determined by what is in the Document or page scripts. The author must therefore be conscious of the run-time state of his Document.

Suppose, for example, on Monday morning, you write a button script that refers to a vector
vec that you just created, say on a command line. The script compiles and works fine. Later, on Tuesday afternoon, you open the Document and try to run the script, but find that you get an error. The vector vec is not defined! This is an easy mistake to make, and an easy one to fix. Global objects that will be used by scripts should be created or loaded in the Document script, or the page script.

This raises an important question of style: which objects and constructs should be global, and which should be local? For example, if a program refers to a global object, then this limits the "portability" of the program. Another Document will not be able to use it correctly unless it imports the global objects the program needs. That would be a bad thing. Programs should be self-contained.

In general, all
commands and programs must themselves be global. Unless they are defined interactively (say, at the command line), they should be created using the command or program protocols in File scripts , or in a pinch, in Document or page scripts. Objects that are intended to be available at all times for the entire Document should also be defined in file scripts, or in Document or page scripts, using the make command.

Global objects may only be created at the top level of a script. Within any block, objects will always be created as local objects.

Scripts differ from programs and commands in an important way in this regard. It is not possible to define a command or a program within another command or program. Commands and programs may only be defined either interactively, or in scripts. An object created within a program or command will not be installed in the lexicon, and will only be visible while the program or command is executing. We say that such objects have local scope. Their scope is local to the program's duration.

Within Document, page, file, or gadget scripts, the situation is different. There are three commands for building new objects: the make command, the let command, and the creation operator := . They have essentially the same syntax, but their behaviors are quite different.

If you use the
make command (or the := operator) to create an object within a script, then the object has global scope. It is installed in the lexicon. If you use the let command to create an object within a script, then the object has local scope. It is not installed in the lexicon, and it is visible only while the script is being executed. The := operator is interpreted in the same way as make is.

From the viewpoint of scripting style, it is better to use local objects than to use global ones, when that is possible. Thus it is advised that authors use the
let command in their scripts rather than the make command or := operator, unless the object must persist beyond the duration of the script. The behavior of the make command and := operator within the body of programs or commands is (as was pointed out) the same as the behavior of let. No new global objects can be created within the bodies of commands or programs.