The Hyper Programming Language |
home | language reference | compiler | hyper on launchpad |
Variables are well known if you already know a traditional (imperative) or an object-oriented programming language. The word `variable' in Hyper is used only to refer to variables that are declared inside a procedure or a constructor. A field inside a class is not called a `variable'. A variable can be declared anywhere a statement is allowed. So in a way you can view a variable declaration as a statement.
A variable is declared on one line, like a simple statement. It starts with the var keyword, followed by the name of the variable, a colon and then the type:
var name : type
You can initialize the variables directly with an initializer expression:
var name : type = initializer
You can create several variables that share their types and initial values. For two variables:
var name1 & name2 : type = initializer
This can of course be generalized for more variables:
var name1 & name2 & name3 & name4 & name5 : type = initializer
The initializer is always optional, even if you declare more than one variable at once. If you don't specify an initializer, then the variables are initialized to the default value for their type.
A variable can be declared constant in two ways: either via the type or by putting the const keyword in front of the declaration. In this second case the var keyword even becomes optional. A constant variable must be initialized directly:
var name : const rest_of_type = initializer const var name : rest_of_type = initializer const name : rest_of_type = initializer
As said earlier, a variable declaration may occur everywhere between statements, as it is in C++ and Java. Notice that the declaration is not ended with a semicolon; Hyper doesn't use the semicolon to terminate every statement or variable declaration.
A simple starting example:
procedure p() var x : int x = 10 var y : int y = 10 var z : byte z = 8 end
The previous can be shortened to:
procedure p() var x : int = 10 var y : int = 10 var z : byte = 8 end
And that can be shortened again, to this:
procedure p() var x & y : int = 10 var z : byte = 8 end