Haxe iteration on dynamic

I have a type variable Dynamicand I know for sure that one of its fields, let's call it a, is actually an array. But when I write

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

I get a compilation error in the second line:

You cannot iterate over a dynamic value, please specify Iterator or Iterable

How can I make this compile?

+5
source share
2 answers

Haxe cannot iterate over Dynamicvariables (as the compiler says).

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

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

You can also change Dynamicto the type of the contents of the array.

+5
source

- :

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

Source: https://habr.com/ru/post/1696753/


All Articles