Assignments and Variables
A variable is a named item whose value may be changed during the course of execution of a NetRexx program. The process of changing the value of a variable is called assigning a new value to it.
Each variable has an associated type, which cannot change during the execution of a program; therefore, the values assigned to a given variable must always have a type that can safely be assigned to that variable.
Variables may be assigned a new value by the method or parse instructions, but the most common way of changing the value of a variable is by using an assignment instruction. Any clause within a class and of the form:
assignment;
where assignment is:
term=expression
is taken to be an assignment instruction. The result of the expression becomes the new value of the variable named by the term to the left of the equals sign. When the term is simply a symbol, this is called the name of the variable.
Example:
/* Next line gives FRED the value 'Frederic' */
fred='Frederic'
The symbol naming the variable cannot begin with a digit (0-9).(1)
Within a NetRexx program, variable names are not case-sensitive (for example, the names fred, Fred, and FRED refer to the same variable). Where public names are exposed (for example, the names of properties, classes, and methods, and in cross-reference listings) the case used for the name will be that used when the name was first introduced ('first' is determined statically by position in a program rather than dynamically).
Similarly, the type of a NetRexx variable is determined by the type of the value of the expression that is first assigned to it.(2) For subsequent assignments, it is an error to assign a value to a variable with a type mismatch unless the language processor can determine that the value can be assigned safely to the type of the variable.
In practice, this means that the types must match exactly, be a simplification, or both be 'well-known' types such as Rexx, String, int, etc., for which safe conversions are defined. The possibilities are described in the section on Conversions.(3)
For example, if there are types (classes) called ibm.util.hex, RunKnown, and Window, then:
hexy=ibm.util.hex(3) -- 'hexy' has type 'ibm.util.hex'
rk=RunKnown() -- 'rk' has type 'RunKnown'
fred=Window(10, 20) -- 'fred' has type 'Window'
s="Los Lagos" -- 's' has type 'Rexx'
j=5 -- 'j' has type 'Rexx'
The first three examples invoke the constructor method for the type to construct a value (an object). A constructor method always has the same name as the class to which it belongs, and returns a new value of that type. Constructor methods are described in detail in Methods and Constructors.
The last two examples above illustrate that, by default, the types of literal strings and numbers are NetRexx strings (type Rexx) and so variables tend to be of type Rexx. This simplifies the language and makes it easy to learn, as many useful programs can be written solely using the powerful Rexx type. Potentially more efficient (though less human-oriented) primitive or built-in types for literals will be used in binary classes.
If the examples above were in a binary class, then, in the reference implementation, the types of s and j would have been java.lang.String and int respectively.
A variable may be introduced ('declared') without giving it an initial value by simply assigning a type to it:
i=int
r=Rexx
f=java.io.File
Here, the expression to the right of the '=' simply evaluates to a type with no value.
The use and scope of variables
NetRexx variables all follow the same rules of assignment, but are used in different contexts. These are:
- Properties
Variables which name the values (the data) owned by an object of the type defined by the class are called properties. When an object is constructed by the class, its properties are created and are initialized to either a default value (null or, for variables of primitive type, an implementation-defined value, typically 0) or to a value provided by the programmer.
The attributes of properties can be changed by the properties instruction. For example, properties may also be constant, which means that they are initialized when the class is first loaded and do not change thereafter.
- Method arguments
When a method is invoked, arguments may be passed to it. These method arguments are assigned to the variables named on the method instruction that introduces the method.
- Local variables
Variables that are known only within a method are called local variables; each time a method is invoked a distinct set of local variables is available. Local variables are normally given an initial value by the programmer. If they are not, they are initialized to a default value (null or, for variables of primitive type, an implementation-defined value, typically 0).
In order for types to be determined and type-checking to be possible at 'compile-time', and easily determined by inspection, the use and type of every variable is determined by its position in the program, not by the order in which assignments are executed. That is, variable typing is static.
The visibility of a variable depends on its use. Properties are visible to all methods in a class; method arguments and local variables are only visible within the method in which they appear. In particular:
To summarize: a symbol that names a variable in the current class either refers to a property (and in any use of it within the class refers to that property), or it refers to a variable that is unique within a method (and any use of the name within that method refers to the same variable).
Note: A variable is just a name, or 'handle' for a value. It is possible for more than one variable to refer to the same value, as in the program:
first='A string'
second=first
Here, both variables refer to the same value. If that value is changeable then a change to the value referred to by one of the variable names would also be seen if the value is referred to by the other. For example, sub-values of a NetRexx string can be changed, using Indexed references, so a change to a sub-value of first would also be seen in an identical indexed reference to second.
Terms on the left of assignments
In an assignment instruction, the term to the left of the equals sign is most commonly a simple non-numeric symbol, which always names a variable in the current class. The other possibilities, as seen in the example below, are:
- The term is an indexed reference, to an existing variable that refers to a string of type Rexx or an array. The variable may be in the current class, or be a property in a class named in the uses phrase of the class instruction for the current class.
- The term is a compound term that ultimately refers to a property (see above) in some class (which may be the current class). This property cannot be a constant.
Examples:
r=Rexx ''
r['foo']='?' -- indexed string assignment
s=String[3]
s[0]='test' -- array assignment
Sample.value=1 -- property assignment
this.value=1 -- property assignment
super.value=1 -- property assignment
The last two examples show assignments to a property in the current class or in a superclass of the current class, respectively. Note that references to properties in other classes must alway be qualified in some way (for example, by the prefix super.). The use of the prefix this. for properties in the current class is optional.