Function optional argument catching with subclasses

154 views
Skip to first unread message

Marcin Kotz

unread,
Nov 29, 2016, 4:12:45 AM11/29/16
to Haxe
I have a function with a following signature:

public function on<T>(?onEvent:js.html.Event->Void, ?onCustom:T->Void, ?onVoid:Void->Void){}

Passing Event works as expected (1st argument), passing anything else works as expected (2nd argument) and passing an empty listener also works (3rd argument).

BUT passing js.html.MouseEvent, js.html.TouchEvent does not work, even though they exetend js.html.Event - they are caught by the 2nd argument, where I would like them to be in the 1st one.

I can't detect event type from onCustom since T type is removed at compilation.

Is this behaviour normal, or a bug? Any workarounds?

David Holaň

unread,
Dec 4, 2016, 6:52:49 AM12/4/16
to haxe...@googlegroups.com
The problem is that a method expecting js.html.Event->Void argument cannot handle js.html.MouseEvent->Void argument. The latter cannot be used in all places the former can be.

For example this valid code:

touchEvent : js.html.TouchEvent;  // we have a TouchEvent lying around for some reason

public function on<T>(?onEvent:js.html.Event->Void) {
    onEvent(touchEvent);
}

We are asking for a generic event listener and then pass a touch event to it. If someone passed js.html.MouseEvent->Void listener instead, then that listener would not be compatible with the touch event argument.

Macros would probably solve your problem, whatever it is. For example macro function can inspect compile-time type of argument passed to it and then decide what code should be used to handle it. See https://haxe.org/manual/macro-arguments.html

-David Holan

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

PSvils

unread,
Dec 4, 2016, 11:01:29 AM12/4/16
to Haxe
This is untested, but perhaps you can try this approach:
public function on<U:js.html.Event, T>(?onEvent:U->Void, ?onCustom:T->Void, ?onVoid:Void->Void){}
Depending on how you're planning to use the onEvent parameter, this could work, but everything David said still applies, this will just enable you to do some things, and again prevent you from doing other things.

Marcin Kotz

unread,
Dec 5, 2016, 5:51:01 AM12/5/16
to Haxe
That's a great hint, but unfortunately onEvent:U catches also NON js.html.Event listeners, which definetly looks like a bug :/

PSvils

unread,
Dec 5, 2016, 3:31:10 PM12/5/16
to Haxe
In that case it would be worth reporting it on Haxe's Github issues! Just remember to provide a fast gist / code that can be compiled to demonstrate the problem.
(Post a link to the issue here, or I can try it and report it if you don't use Github)

Marcin Kotz

unread,
Dec 9, 2016, 9:19:39 AM12/9/16
to Haxe
I have finally found time to add this issue officially: https://github.com/HaxeFoundation/haxe/issues/5870

Marcin Kotz

unread,
Dec 15, 2016, 8:52:04 AM12/15/16
to Haxe
Unfortunately this has been resolved as a no-issue, since Haxe 4 is going to drop optional arguments skipping :/
Reply all
Reply to author
Forward
0 new messages