Mateusz Sowiński
Mateusz Sowiński

Reputation: 439

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?

Upvotes: 2

Views: 139

Answers (1)

Gama11
Gama11

Reputation: 34138

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

Upvotes: 4

Related Questions