views:

482

answers:

2

Hi, I know it's unlikely but maybe there is someone who knows haXe language. I have a variable of Dynamic type and I know for sure one of it's fields, lets call it an 'a' actually is an array. But when I'm writing

var d : Dynamic = getDynamic();
for (t in d.a) {
}

I get a compilation error on line two, saying 'You can't iterate on a Dynamic value, please specify Iterator or Iterable'. How can I make this compilable?

+3  A: 

haXe can't iterate over Dynamic variables (as the compiler says).

You can make it work in several ways, where this one is probably easiest (depending on your situation):

var d : {a:Array<Dynamic>} = getDynamic();
for(t in d.a) { ... }

You could also change Dynamic to the type of the contents of the array.

PS: yay haXe!

Danny Wilson
A: 

Another way to do the same is to use an extra temp variable and explicit typing:

var d = getDynamic();
var a: Array<Dynamic> = d.a;
for (t in a) { ... }
Michael Pliskin