19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

declares id to be a mutable variable of type Type whose initial value is computed to be the value of the expression<br />

expr . As before, the static type of expr must be a subtype of Type. <strong>The</strong> modifier var is optional when ‘ := ’ is used<br />

instead of ‘ = ’ as follows:<br />

[var] id : Type := expr<br />

<strong>The</strong> first three forms are referred to as variable definitions. <strong>The</strong> fourth form:<br />

[var] id : Type<br />

declares a variable without giving it an initial value (where mutability is determined by the presence of the var<br />

modifier). It is a static error if a variable is referred to before it has been given a value; an immutable variable is<br />

initialized by another variable declaration and a mutable variable is initialized by assignment. It is also a static error<br />

if an immutable variable is initialized more than once. Whenever a variable bound in this manner is assigned a value,<br />

the type of that value must be a subtype of its declared type. This form allows declaration of the types of variables<br />

to be separated from definitions, and it allows programmers to delay assigning to a variable before a sensible value is<br />

known.<br />

In short, immutable variables are declared and initialized by ‘ = ’ and mutable variables are declared and initialized by<br />

‘ := ’ except when they are declared as the third form above with the modifier var .<br />

All forms can be used with tuple notation to declare multiple variables together. Variables to declare are enclosed in<br />

parentheses and separated by commas, as are the types declared for them:<br />

(id[,id] + ) : (Type[, Type] + )<br />

Alternatively, the types can be included alongside the respective variables, optionally eliding types that can be inferred<br />

from context (see Chapter 20 for a discussion of type inference in <strong>Fortress</strong>):<br />

(id[:Type][,id[: Type]] + )<br />

Alternatively, a single type followed by ‘ . . . ’ can be declared for all of the variables:<br />

(id[,id] + ):Type...<br />

This notation is especially helpful when a function application returns a tuple of values.<br />

Here are some simple examples of variable declarations:<br />

π = 3.141592653589793238462643383279502884197169399375108209749445923078<br />

declares the variable π to be an approximate representation of the mathematical object π . It is also legal to write:<br />

π : R64 = 3.141592653589793238462643383279502884197169399375108209749445923078<br />

This definition enforces that π has static type R64 .<br />

In the following example, the declaration of the type of a variable and its definition are separated:<br />

π : Float<br />

π = 3.141592653589793238462643383279502884197169399375108209749445923078<br />

<strong>The</strong> following example declares multiple variables using tuple notation:<br />

var (x, y) : Z64 . . . = (5, 6)<br />

<strong>The</strong> following three declarations are equivalent:<br />

(x, y, z) : (Z64, Z64, Z64) = (0, 1, 2)<br />

(x: Z64, y: Z64, z: Z64) = (0, 1, 2)<br />

(x, y, z) : Z64 . . . = (0, 1, 2)<br />

Special syntax is provided for declaring variables to be parts of a matrix, as described in Section 6.5.<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!