简体   繁体   English

Haxe-FOR作为内联表达式

[英]Haxe - FOR as an inline expression

Is it possible to do something like this? 有可能做这样的事情吗?

trace( for(a in array) a );

I've seen it used when populating an array: 我看过它在填充数组时使用:

var numbers = [ for (i in 0...100) i ];

But doesn't seem to work as an overall expression? 但是似乎不能作为一个整体来表达吗?

for can be used "as a value" in array comprehension (as you mentioned) as well as map comprehension . for可以在数组解析 (如您提到的)以及映射解析中 “用作值”。 The same is true for while and do...while . 对于whiledo...while也是如此。

In other places, loops cannot be used like this. 在其他地方,不能像这样使用循环。 Everything is an expression explains this well, using pretty much the same trace example you gave: 一切都是一个表达式 ,它使用与您给出的几乎相同的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. 某些表达式(例如循环或var声明)作为值没有任何意义,因此它们将被键入为Void,因此将无法在需要值的地方使用。 For example the following won't compile: 例如,以下内容将不会编译:

 trace(for (i in 0...10) i); // ERROR: Cannot use Void as value 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM