Error on externs

27 views
Skip to first unread message

Luiz Paulo Ferreira dos Santos Brandão

unread,
Aug 5, 2014, 11:50:28 PM8/5/14
to haxe...@googlegroups.com
I have the following files:

test.js
window.Test = {
  length
: function() {}
};

Test.hx
package ;

extern class Test {
 
static function length():Void;
}

Main.hx
package ;

class Main {
 
static function main():Void {
   
Test.length();
 
}
}


When I compile Main.hx outputs it:
(function () { "use strict";
var Main = function() { };
Main.main = function() {
 
Test.$length();
};
Main.main();
})();

My question is why my function .length is compiled to ".$length" ?

ps: sorry for my english.

Andy Li

unread,
Aug 6, 2014, 12:59:42 AM8/6/14
to haxe...@googlegroups.com
It sounds like a bug so I've just reported it -> https://github.com/HaxeFoundation/haxe/issues/3229

Reason of why this is happening:
Haxe "class object" is compiled to a Function object in JS. For example the Main class in your example: "var Main = function() { };"
In JS, Function has an instance method "length". To avoid causing conflict, haxe renamed static length method to "$length".

Workaround is to use __js__ magic with inline:
extern class Test {
inline static function length():Int return untyped __js__("Test.length()");
}

Hope it helps.

Best,
Andy


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Luiz Paulo Ferreira dos Santos Brandão

unread,
Aug 6, 2014, 9:44:59 AM8/6/14
to haxe...@googlegroups.com
Thanks, Andy!
Reply all
Reply to author
Forward
0 new messages