Scope: Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Created page with "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 i...")
 
m (minor formatting)
Line 6: Line 6:
*A method or complex subroutine scope.
*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:
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
<p class="code">begin
   %x  is float
   %x  is float
   %x  is string len 8
   %x  is string len 8
   %x = 'Hello'
   %x = 'Hello'
  end
end  
</p>
Similarly, the following is also invalid:
Similarly, the following is also invalid:
  local subroutine (string):silly
<p class="code">local subroutine (string):silly
    %x  is float
  %x  is float
    %x  is string len 8
  %x  is string len 8
    %x = 13
  %x = 13
  end subroutine
end subroutine
</p>
Hopefully, the reasons for this restriction are obvious.
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:
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
<p class="code">begin
   %x  is float
   %x  is float
   &nbsp;
   &nbsp;
Line 29: Line 31:
   &nbsp;
   &nbsp;
   %x = 'Hello'
   %x = 'Hello'
  end
end
</p>


==Variables==
==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:
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
<p class="code">%foo    is float
  %bar    object stringlist
%bar    object stringlist
</p>
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.
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.
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.

Revision as of 17:46, 20 March 2018

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 user Language:

  • 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.