Chapel variables are declared using the var keyword. As an
example, the following statement declares a variable named x of
integer type (
int) and initializes it to 42:
var x: int = 42;
We can verify its value by printing it out using
writeln("x is ", x);
Compiling and running this program results in:
x is 42
As we will see in the following sections, a variable declaration may omit its initializer or its type, but not both.
If a variable declaration does not contain an initializer, the compiler will initialize the variable to a default value based on its type. For example, the default value for integers is zero, so if we had instead declared x as:
var x: int; writeln("x is ", x);
We would get:
x is 0
Alternatively, a variable declaration with an initializer may omit its type specification. In this case, the compiler will infer the variable’s type to be the same as its initialization expression’s. Thus, we could have written the original program simply as:
var x = 42; writeln("x is ", x);
In this case, the compiler knows that 42 is a value of integer type
int), therefore it infers that the type of x is also
Running this program, once again produces:
x is 42
In practice, this style of coding is often used for brevity, resulting in a scripting-like feel. Note, however, that unlike most scripting languages, a Chapel variable’s type is determined at compile-time and fixed for its lifetime. Thus, in each of these three declaration styles, x will be an integer for its lifetime and cannot be used to store values other than integers.
Compound Variable Declarations¶
The var keyword can also be used to create more than one variable at a time. For example, the following statement will create three integer variables, a, b, and c, all initialized to 42:
var a, b, c: int = 42;
As before, initialization and type information may be omitted. For example, the following statement creates three integer variables, i, j, and k, all default-initialized to zero:
var i, j, k: int;
Similarly, the following declaration creates three variables, x,
y, and z, all inferred to be of type
int due to being
initialized with the integer 42:
var x, y, z = 42;
Finally, a single variable declaration statement may mix several type and initializer expressions, separating them by commas. For example, the following statement declares a, b, and c to be integer variables initialized to 42; i, j, and k to be integer variables default-initialized to zero; s and t to be string variables initialized to “hi”; and x, y, and z to be inferred integer variables initialized to 42:
var a, b, c: int = 42, i, j, k: int, s, t = "hi", x, y, z = 42;
(Note that whitespace doesn’t matter in Chapel, so the use of linefeeds in this example is simply for readability and has no practical impact).
The rule of thumb is that any variable which has neither a type specification nor an initializer will share the type and/or initializer expressions of the next variable which has one or both. Thus, in the example above, a and b share c’s type and initializer; i and j share k’s type; s shares t’s initializer; and x and y share z’s initializer.
Needless to say, the last variable in a compound declaration must have a type and/or initializer.