js externs default parameter

131 views
Skip to first unread message

Vincent Blanchet

unread,
Aug 29, 2017, 6:33:58 AM8/29/17
to Haxe
Hi,

I'm currently trying to unify some code between an openfl API and a js api.
I'm facing an issure regarding a function that takes 3 parameters in js and 2 in openfl :

js extern:
public function addEventListener(evtType:String, listener:Dynamic, scope:Dynamic)
openfl :
public function addEventListener(evtType:String, listener:Dynamic);

I want a default value for the third parameter in the js extern function signature. The best should be that it could be replaced by "this" in generated code.

I tryed this :
function addEventListener(evtType:String, listener:Dynamic, scope:Dynamic= untyped __js__('this')):Void;
But nothing shows in the output.

I currently don't know how to set a default value, I tryed something more simple to check the output :
public function addEventListener(evtType:String, listener:Dynamic, scope:String="something");
But the generated code doesn't include "something"

Can you explain me how i can achieve this? 
I'm really not familiar with macros if this this the answer :) 

Regards
--
Vincent BLANCHET,
__________________________________
Developer
Skype : vincent.blanchet.info

Garanti sans virus. www.avast.com

Michel Romecki

unread,
Aug 29, 2017, 10:52:28 AM8/29/17
to haxe...@googlegroups.com

Hej Vincent,
If I understood well, you can do what you want without macro, just using "inline" keyword in order to "override" the signature and the body if needed.

--
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.

Vincent Blanchet

unread,
Aug 30, 2017, 9:32:31 AM8/30/17
to Haxe
Great!

Thank you Michel, solution look at this :

    inline function addEventListener(evtType:String, listener:Dynamic, ?scope:Dynamic):Void
    {
        addEventListener(evtType,listener,untyped __js__('this'));
    }

Best regards

Vincent Blanchet

unread,
Sep 28, 2017, 8:18:07 AM9/28/17
to Haxe
Hey

I'm still trying to unify two apis and I am now stuck on this : Is it possible to change the constructor of an extern?
For instance here I want to modify the extern constructor to get the same argument order than the AS API

AS api :
class Foo{
       public function new(a:Int, b:Int, c:Int) { ... }
}

JS api : 
@:native("Foo")
extern class Foo{
       function new(a:Int, c:Int, b:Int);
}

Any ideas?

Best regards,

Garanti sans virus. www.avast.com

Vincent Blanchet

unread,
Oct 24, 2017, 6:20:02 AM10/24/17
to Haxe
Check the html5 extern constructor, it's not the same as the haxe class.
Currently if I use it I need to write this :
            #if html5
            sfs.send(new LoginRequest(nick, null, null, "SimpleMMOWorld2"));
            #else
            sfs.send(new LoginRequest(nick, null, "SimpleMMOWorld2"));
            #end
I want to avoid compilation flags when using the library. I want the compilation complexity hidden in the class itself.
Is there a way to unify it by keeping the haxe class constructor without changing the js code?
By returning the js constructor in the haxe code with good parameter order (if possible )?
Using macros?
Anything?

Best regards


2017-09-28 14:18 GMT+02:00 Vincent Blanchet <vincent.bl...@gmail.com>:
Hey

I'm still trying to unify two apis and I am now stuck on this : Is it possible to change the constructor of an extern?
For instance here I want to modify the extern constructor to get the same argument order than the AS API

AS api :
class Foo{
       public function new(a:Int, b:Int, c:Int) { ... }
}

JS api : 
@:native("Foo")
extern class Foo{
       function new(a:Int, c:Int, b:Int);
}

Any ideas?

Best regards,

Garanti sans virus. www.avast.com
Reply all
Reply to author
Forward
0 new messages