Definition sanity checking
Factor handbook » The implementation » Definitions

Prev:Definition protocol
Next:Compilation units

When a source file is reloaded, the parser compares the previous list of definitions with the current list; any definitions which are no longer present in the file are removed by a call to forget.

The parser also catches forward references when reloading source files. This is best illustrated with an example. Suppose we load a source file a.factor:
USING: io sequences ; IN: a : hello ( -- str ) "Hello" ; : world ( -- str ) "world" ; : hello-world ( -- ) hello " " world 3append print ;

The definitions for hello, world, and hello-world are in the dictionary.

Now, after some heavily editing and refactoring, the file looks like this:
USING: make ; IN: a : hello ( -- ) "Hello" % ; : hello-world ( -- str ) [ hello " " % world ] "" make ; : world ( -- ) "world" % ;

Note that the developer has made a mistake, placing the definition of world after its usage in hello-world.

If the parser did not have special checks for this case, then the modified source file would still load, because when the definition of hello-world on line 4 is being parsed, the world word is already present in the dictionary from an earlier run. The developer would then not discover this mistake until attempting to load the source file into a fresh image.

Since this is undesirable, the parser explicitly raises a no-word error if a source file refers to a word which is in the dictionary, but defined after it is used.

The parser also catches duplicate definitions. If an artifact is defined twice in the same source file, the earlier definition will never be accessible, and this is almost always a mistake, perhaps due to a bad choice of word names, or a copy and paste error. The parser raises an error in this case.