Top level forms - Factor Documentation (original) (raw)

Any code outside of a definition is known as a top-level form; top-level forms are run after the entire source file has been parsed, regardless of their position in the file.

Top-level forms cannot access the parse-time manifest (Reflection support for vocabulary search path), nor do they run inside with-compilation-unit; as a result, meta-programming might require extra work in a top-level form compared with a parsing word.

Also, top-level forms run in a new dynamic scope, so using set to store values is almost always wrong, since the values will be lost after the top-level form completes. To save values computed by a top-level form, either use set-global or define a new word with the value.