Scope: Difference between revisions
m (minor formatting) |
m (add category) |
||
Line 1: | Line 1: | ||
[[User Language]] entities such as variables, complex subroutines, classes, and methods have what is referred to as a ''scope''. The term scope refers to two things: | SOUL (formerly [[User Language]]) entities such as variables, complex subroutines, classes, and methods have what is referred to as a ''scope''. The term scope refers to two things: | ||
<ul> | |||
<li>The scope in which an entity is declared or defined </li> | |||
In general, there are two scopes in | <li>The scope in which an entity can be referenced </li> | ||
</ul> | |||
In general, there are two scopes in SOUL: | |||
<ul> | |||
<li>The outermost scope; specifically the scope directly inside the Begin/End block and not inside a complex subroutine or method. </li> | |||
<li>A method or complex subroutine scope. </li> | |||
</ul> | |||
<p> | |||
Only one entity of a particular type (variable, method, class, etc.) and name can be defined within a scope. For example, the following is '''not'' allowed: | Only one entity of a particular type (variable, method, class, etc.) and name can be defined within a scope. For example, the following is '''not'' allowed: | ||
</p> | |||
<p class="code">begin | <p class="code">begin | ||
%x is float | %x is float | ||
Line 17: | Line 24: | ||
%x is string len 8 | %x is string len 8 | ||
%x = 13 | %x = 13 | ||
end subroutine | end subroutine </p> | ||
<p> | |||
Hopefully, the reasons for this restriction are obvious. </p> | |||
<p> | |||
It is perfectly valid, however, for like entities to be defined or declared with the same names in different scopes. For example, the following is perfectly valid: | |||
</p> | </p> | ||
<p class="code">begin | <p class="code">begin | ||
%x is float | %x is float | ||
Line 42: | Line 50: | ||
Model 204 images and screens can also be though of as variables though their declaration is different from a variable declaration. As such, an image or screen declared with a scope can only be referenced in that scope. | Model 204 images and screens can also be though of as variables though their declaration is different from a variable declaration. As such, an image or screen declared with a scope can only be referenced in that scope. | ||
[[Category:SOUL]] |
Latest revision as of 18:48, 23 April 2018
SOUL (formerly User Language) entities such as variables, complex subroutines, classes, and methods have what is referred to as a scope. The term scope refers to two things:
- The scope in which an entity is declared or defined
- The scope in which an entity can be referenced
In general, there are two scopes in SOUL:
- The outermost scope; specifically the scope directly inside the Begin/End block and not inside a complex subroutine or method.
- A method or complex subroutine scope.
Only one entity of a particular type (variable, method, class, etc.) and name can be defined within a scope. For example, the following is 'not allowed:
begin %x is float %x is string len 8 %x = 'Hello' end
Similarly, the following is also invalid:
local subroutine (string):silly %x is float %x is string len 8 %x = 13 end subroutine
Hopefully, the reasons for this restriction are obvious.
It is perfectly valid, however, for like entities to be defined or declared with the same names in different scopes. For example, the following is perfectly valid:
begin %x is float local subroutine (string):silly %x is float %x = 13 end subroutine %x = 'Hello' end
Variables
Variable names always begin with a percent (%) symbol and must be either explicitly or implicitly declared within the outermost (Begin/End) or method/complex subroutine scope. Explicit declaration is accomplished by starting a line with the name of the variable, optionally followed by the word Is, followed by the type of the variable (Float, String, Object, etc.), as in:
%foo is float %bar object stringlist
Variables are declared implicitly the first time they are referenced within a scope. Implicitly declared variables will always have the same type (as indicated by the Model 204 VTYPE user parameter). If VTYPE is set to UNDEFINED or the request has a VARIABLES ARE UNDEFINED compiler directive, no implicit declarations will be done. In any case, once a variable is declared inside a scope, the name can be used anywhere else inside the scope to reference the variable. That variable will not be available outside the declaring scope.
Model 204 images and screens can also be though of as variables though their declaration is different from a variable declaration. As such, an image or screen declared with a scope can only be referenced in that scope.