The reason this does not work is because basic types such as Int are immutable. In fact, the variables a , b , c and d may even exist, since they never change. If you look at the JS Source tab on try.haxe.org , you will notice that they are not present in the output:
var array1 = [0,0,0,0]; var array2 = [1,2,3,4]; var _g = 0; while(_g < 4) { var i = _g++; array1[i] = array2[i]; } console.log(0); console.log(0); console.log(0); console.log(0);
This is an analyzer optimization.
To do this, you will need some type of container, such as class or typedef . Here is an example of using the latter:
class Test { static function main() { var a:IntContainer = { value: 0 }; var b:IntContainer = { value: 0 }; var c:IntContainer = { value: 0 }; var d:IntContainer = { value: 0 }; var array1:Array<IntContainer> = [a, b, c, d]; var array2:Array<Int> = [1, 2, 3, 4]; for (i in 0...4) array1[i].value = array2[i]; trace(a.value); trace(b.value); trace(c.value); trace(d.value); } } typedef IntContainer = { value:Int }
source share