Search code examples
flashhaxe

What's the meaning of __new__ in Haxe for Flash?


I saw some code (written for Flash) that looks like this:

var _array:Array<Int> = untyped __new__(Array, 10);

And I find it creates an Array and with a length of 10. However, when I try something like:

var _array:Array<Int> = untyped __new__(Array, 1, 2);

It creates an Array which has two elements. I'm confused by this, what's the real meaning of __new__?


Solution

  • It is Haxe "magic", see the wiki.

    The __new__ is to call the native constructor, in the case of Array, it is different from what Haxe exposes (Array in Haxe doc vs Array in AS3 doc).

    Basically using __new__ initializes the Array with a length and/or values without manually doing:

    var array = [];
    for(i in 0...len) array.push(0);