Haxe - FOR 作为内联表达式
Haxe - FOR as an inline expression
是否可以这样做?
trace( for(a in array) a );
我看到它在填充数组时使用过:
var numbers = [ for (i in 0...100) i ];
但似乎不能作为一个整体表达?
for
可以在 array comprehension (as you mentioned) as well as map comprehension 中使用 "as a value"。 while
和do...while
也是如此。
其他地方不能这样使用循环。 Everything is an expression 很好地解释了这一点,使用与您给出的几乎相同的 trace
示例:
Some expressions, such as loops or var declarations don't make any sense as values, so they will be typed as Void and thus won't be able to be used where value is expected. For example the following won't compile:
trace(for (i in 0...10) i); // ERROR: Cannot use Void as value
是否可以这样做?
trace( for(a in array) a );
我看到它在填充数组时使用过:
var numbers = [ for (i in 0...100) i ];
但似乎不能作为一个整体表达?
for
可以在 array comprehension (as you mentioned) as well as map comprehension 中使用 "as a value"。 while
和do...while
也是如此。
其他地方不能这样使用循环。 Everything is an expression 很好地解释了这一点,使用与您给出的几乎相同的 trace
示例:
Some expressions, such as loops or var declarations don't make any sense as values, so they will be typed as Void and thus won't be able to be used where value is expected. For example the following won't compile:
trace(for (i in 0...10) i); // ERROR: Cannot use Void as value