简体   繁体   中英

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 . The same is true for while and do...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:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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