Scope of declarations
Scope is the context in which a variable, procedure, class, or type is declared. Scope affects the accessibility of an item's value outside that context. For example, variables declared within a procedure are typically not available outside of the scope of that procedure.
LotusScript® recognizes three kinds of scope:
- Module scope
- Procedure scope
- Type or class scope
Name conflicts and shadowing
Two variables or procedures with the same name cannot be declared in the same scope. The result is a name conflict. The compiler reports an error when it encounters a name conflict in a script.
Variables or procedures declared in different scopes can have the same name. LotusScript® interprets the name as referring to the variable or procedure declared in the innermost scope that is visible where the reference is used.
A variable or procedure of the same name declared at a scope outside of this innermost visible scope is not accessible. This effect is called shadowing: the outer declaration(s) of the name are shadowed, or made invisible, by the inner declaration.
Module scope
A variable is declared in module scope if the declaration is outside of any procedure, class, or type definition in the module. The variable name has a meaning as long as the module is loaded.
The variable name is visible anywhere within the module and has the meaning specified in the declaration, except within a procedure, type, or class where the same variable name is also declared.
The variable is Private by default and can be referred to only within the module that defines it. A variable can be referred to in other modules only if it is declared as Public and the other modules access the defining module with the Use statement.
The following situations result in a name conflict across modules:
- Two Public constants, variables, procedures, types, or classes with the same name
- A Public type with the same name as a Public class
- A Public module-level variable with the same name as a Public module-level constant or procedure
- A Public module-level constant with the same name as a Public module-level procedure
The following situations result in a name conflict within a module:
- A type with the same name as a class
- A module-level variable with the same name as a module-level constant or procedure
- A module-level constant with the same name as a module-level procedure
Procedure scope
A variable is declared in procedure scope if it is declared within the definition of a function, a sub, or a property. Only inside the procedure does the variable name have the meaning specified in the declaration. The variable name is visible anywhere within the procedure.
Ordinarily, the variable is created and initialized when the procedure is invoked, and deleted when the procedure exits. This behavior can be modified with the Static keyword:
- If the variable is declared with the Statickeyword, its value persists between calls to the procedure. The value is valid as long as the module containing the procedure is loaded.
- If the procedure itself is declared Static, the values of all variables in the procedure (whether explicitly or implicitly declared) persist between calls to the procedure.
The following situations result in a name conflict within a procedure:
- Two procedure arguments with the same name
- Two labels with the same name
- Two variables with the same name
- A procedure argument and a variable with the same name
- A function that contains a variable or argument of the function name
- A property that contains a variable of the property name
Type or class scope
A variable is declared in type or class scope if it is declared within the definition of a type or a class (for classes, it must additionally be declared outside the definition of a procedure). The variable is called a member variable of the type or class.
- Type member variables: A type member variable is created and initialized
when an instance of that type is declared. It is deleted when the
type instance or instance variable goes out of scope.
The visibility of a type member variable is automatically Public.
- Class member variables: A class member variable is created and
initialized when an instance of that class is created. It is deleted
when the object is deleted.
Each class member variable can be declared Publicor Private. A Private member can only be referred to within the class or its derived classes; class member variables are Private by default.
The visibility of a type member variable (which is always Public) and of a Publicclass member variable depends, for any particular type or object, on the declaration of the instance variable that refers to that instance:
- If the instance variable is declared Private, then the member variable is visible only in the owning module.
- If the instance variable is declared Public, then the member variable is visible wherever the instance variable is visible: it can be referred to in the other modules where the module that owns this instance variable is accessed with the Use statement.
The following situation results in a name conflict within a type:
- Two type members with the same name.
The following situation results in a name conflict within a class:
- Two class members (variables or procedures) with the same name.