Rules for globals usage
Follow the rules described in this topic in order to use globals properly.
Defining global variables in a GLOBALS
block is deprecated. Instead of
GLOBALS
, use PUBLIC
symbols in modules to be imported with IMPORT FGL
. GLOBALS
is
only supported for backward compatibility, to compile legacy source code.
Why not globals? Global variables visible and modifiable across the entire program code is an old
concept that reduces modularity and flexibility. GLOBALS
implements specific
semantics that restrain source code organization; for example, it is allowed to specify a file path
such as GLOBALS "../../common/myglobals.4gl"
. The globals file can define functions
after the GLOBALS
block that are ignored when including such file.
Conversely, the IMPORT FGL
instruction brings more flexibility and allows a
better organization of your source code, by grouping public variables in modules where other symbols
(functions, types, contants) belong to the same domain.
Rules for GLOBALS
usage:
- If you modify the globals file, you must recompile all the modules that include the file.
- If a local element has the same name as another variable that you declare in the
GLOBALS
statement, only the local variable is visible within its scope of reference. - You can declare several
GLOBALS .. END GLOBALS
blocks in the same module. - A source file with
GLOBALS .. END GLOBALS
block must not contain any executable statement. - Do not write a declaration statement outside a
GLOBALS ... END GLOBALS
block in the globals file. - You do not need to compile the source file containing the
GLOBALS
block. However, it is recommended to compile the globals file to detect errors. - You can declare several
GLOBALS "filename"
instructions in the same globals using module. - Although you can include multiple
GLOBALS ... END GLOBALS
statements in the same application, do not declare the same identifier within more than oneGLOBALS
declaration. Even if several declarations of a global elements defined in multiple places are identical, declaring any global element more than once can result in compilation errors or unpredictable runtime behavior. - A
GLOBALS .. END GLOBALS
block can holdGLOBALS "filename"
instructions. In such case, the specified files will be included recursively.