SIMPOL Documentation

Variable and Type Scope and Visibility

Scope and visibility are often a complex topic in programming languages. That is not the case with SIMPOL. In SIMPOL there is only one kind of scope and two kinds of visibility. Before we get into the details, however, it may be useful to explain what these two concepts actually mean. By scope, we generally mean the area of the program where a variable is still in existence and is accessible. BASIC derived languages often have two or more types of scope, global and local being the most common.

Global scope means that the variable is visible and accessible anywhere in the program. It also means that the variable will not be destroyed until the program ends or some statement within the program expressly destroys the variable. Globally visible and accessible variables are often the root of unidentifiable side-effects in complex programs. In a programming language like SIMPOL that is multi-threaded, allowing global variables would be extremely messy, since they would have to be visible in every thread and may change unpredictably depending on how the various threads are scheduled and executing. The alternative would have been to add syntax to lock them which would have added overhead and complexity. There are no global variables in SIMPOL.

Local scope often means within a function, although in some languages it may be only within a block statement, such as a for…next loop that is itself within a function. Local scope in SIMPOL means within a function. From the point in a function where a variable is declared it is visible and remains in existence until the function ends. When the function ends, all of the variables are destroyed, any memory they are using is released and it is as if they had never existed. Variables are not visible outside of a function. although they can be passed as arguments to another function. Technically though, once the function is entered a new local variable is created and the value of the variable in the calling function is assigned to the new local variable which is then in scope until the end of the function at which point its value (which may have changed) is then reassigned to the variable from the original calling function. Static variables are a special form of local variable that retains its value when the function is exited but is only accessible from within the function. There are no static variables in SIMPOL.

Visibility is similar to scope but generally is used to refer to the ability to access type definitions and functions. As described earlier, there are two kinds of visibility in SIMPOL, global and modular. All of the intrinsic types and functions are globally visible. User-defined types and functions are visible only within the same compiled unit unless they have been expressly made globally visible by exporting them using the export keyword. Typically if a program is made up of a main code module plus some linked in libraries (whether self-made or from other source) then the code libraries will make some of their types and functions visible for use by other programs but they may not make all of the types and functions visible unless that is necessary to use the library. There may be only one interface function that is exposed but in actuality there may be a dozen or more functions in the module that are used to implement that exposed function. By only exporting the interface function, the programmer can reduce the level of error checking on the implementation since they don't need to worry about those functions being called from outside the module.