Haxe中的一个块由一个开口的 花括号 { 开始,以一个闭口的花括号 }结束。一个块可以包含一些表达式,每个使用分号结束。通常的语法是:
A block in Haxe starts with an opening curly brace { and ends with a closing curly brace }. A block may contain several expressions, each of which is followed by a semicolon ;. The general syntax is thus:
{
expr1;
expr2;
...
exprN;
}
被块表达式扩展的值和类型等于最后一个子表达式的值和类型。
The value and by extension the type of a block-expression is equal to the value and the type of the last sub-expression.
块可以包含局部变量,通过 var表达式(第5.10节)声明,同样,局部函数通过 function 表达式(第5.11节)声明。它们在块和子块中中是可用的,但是不能在块的范围之外使用。同样,只有在被声明之后才可以使用。下面的例子使用 var,但是同样的规则也适用于 function 的使用:
Blocks can contain local variables declared by var expression (5.10), as well as local functions declared by function expressions (5.11). These are available within the block and within sub-blocks, but not outside the block. Also, they are available only after their declaration. The following example uses var, but the same rules apply to function usage:
{
a; // error, a is not declared yet
var a = 1; // declare a
a; // ok, a was declared
{
a; // ok, a is available in sub-blocks
}
// ok, a is still available after
// sub-blocks
a;
}
a; // error, a is not available outside
在运行时,块从头至尾执行。控制流(如异常(第5.18节)或者返回表达式(第5.19节))可能在所有的表达式被执行之前离开块。
At runtime, blocks are evaluated from top to bottom. Control flow (e.g. exceptions (5.18) or return expressions (5.19)) may leave a block before all expressions are evaluated.