The Freya Programming Language

Var statements

See also

var statements allow the inline declaration of local variables and their initialization.

Syntax

There are two variants of the var statement. The first one includes an explicit type declaration:

var identifier : type := expression
var identifier : type = expression

The alternative syntax omit the type declaration:

var identifier := expression
var identifier = expression

This variant asks the compiler to infer the type of the local variable from the expression's type.

If you need to restrict the scope of an inline variable declaration, you can substitute it with a using statement. All for statement variants also declares local variables with restricted scopes.

Combining declarations in a single statement

Several inline declarations, no matter their types, can be combined in a single var statement:

var cam := new Camera, w := cam.Width, area := w * cam.Height;

Each initializer may use preceding variables from the same statement. This holds because the above statement is translated like this:

var cam := new Camera;
var w := cam.Width;
var area := w * cam.Height;

See also

The Freya Programming Language
Statements
using statements
Expressions