Can you bind to Dynamic?


#1

I am passing a function reference to another function via a dynamic object to be used on an eventListener, together with an Array of Data…

var clickFunc:Dynamic = someFunction;
var dataArray:Array<String> = = ["Home", "Search", "Details", "Options"];
dropDown(someFunction, dataArray);

function dropDown(clickFunc:Dynamic, dataArray:Array<String>){
var dropDownSel:MovieClip = new MovieClip();
dropDownSel.addEventListener(MouseEvent.CLICK, clickFunc.bind(_, dataArray));
}

function someFunction(e:MouseEvent, dataArray:Array<String>=null){

}

bind works when the function referenced in the addEventListener is a straight reference to a function, but not when it’s a Dynamic reference to a function.

Is there a way to implement this?

Thanks


#2

The error relates to the _ event part…

…Unknown identifier : _

This is not a problem for a direct function reference?


#3

Is there a reason the clickFunc variable has to be Dynamic instead of reflecting the parameters and return type?

I did a quick test and got it to work like this:

var clickFunc:MouseEvent->Array<String>->Void = someFunction;
var dataArray:Array<String> = ["Home", "Search", "Details", "Options"];
dropDown(someFunction, dataArray);

function dropDown(clickFunc:MouseEvent->Array<String>->Void, dataArray:Array<String>){
var dropDownSel:MovieClip = new MovieClip();
dropDownSel.addEventListener(MouseEvent.CLICK, clickFunc.bind(_, dataArray));
}

function someFunction(e:MouseEvent, dataArray:Array<String>=null){

}

#4

No reason at all, other than I wasn’t aware this could be done :slight_smile:

I was seeing references to that process but couldn’t get my head around it, your example brought it into perfect focus, and even better it works.

Thanks