HaxeDoc2

(やまだぃちぅ) #1

  • Rvalue functions may have a name, but cannot have type parameters.


5.12 new..............................................


Thenewkeyword signals that a class (2.3) or an abstract (2.8) is being instantiated. It is followed
by the type path (3.7) of the type which is to be instantiated. It may also list explicit type param-
eters (3.2) enclosed in<>and separated by comma,. After an opening parenthesis()follow the
constructor arguments, again separated by comma,, with a closing parenthesis)at the end.

1 class Main{
2 static public function main() {
3 new Main(12, "foo");
4 }
5
6 function new(t:T, s:String){}
7 }


Within themainmethod we instantiate an instance ofMainitself, with an explicit type pa-
rameterIntand the arguments 12 and"foo". As we can see, the syntax is very similar to the
function call syntax (5.9) and it is common to speak of “constructor calls”.

5.13 for...............................................


Haxe does not support traditional for-loops known from C. Itsforkeyword expects an opening
parenthesis(, then a variable identifier followed by the keywordinand an arbitrary expres-
sion used as iterating collection. After the closing parenthesis)follows an arbitrary loop body
expression.

1 for (v in e1) e2;


The typer ensures that the type ofe1can be iterated over, which is typically the case if it has
aniteratormethod returning anIterator<T>, or if it is anIterator<T>itself.
Variablevis then available within loop bodye2and holds the value of the individual ele-
ments of collectione1.
The type of aforexpression is alwaysVoid, meaning it has no value and cannot be used as
right-side expression.
The control flow of loops can be affected bybreak(5.20) andcontinue(5.21) expressions.

5.14 while


A normal while loop starts with thewhilekeyword, followed by an opening parenthesis(, the
condition expression and a closing paranthesis). After that follows the loop body expression:

1 while(condition) expression;


The condition expression has to be of typeBool.
Upon each iteration, the condition expression is evaluated. If it evaluates tofalse, the loop
stops, otherwise it evaluates the loop body expression.

1 class Main {
2 static public function main() {
3 var f = 0.0;
4 while (f < 0.5) {

Free download pdf