package myapp;
//import haxe.Constraints.Function;
import flash.display.*;
import openfl.utils.Function;
class Main extends Sprite {
public var a: Function;
public function new() {
super();
a = new Function();
a = function (val: Float): String {
return "abc";
};
a(123);
}
}
There’s no reason to use Function here to begin with, you should use a proper function type instead. In this case where the function takes a Float and returns a String, it would look like this:
Is their any way I can create an instance of “a” as it is a function ?
public var a:Float -> String
The reason I want to store the instance of function instead of assigning it as a value, is so that I can assign a new function to addEventListener. Like this:
package myapp;
//import haxe.Constraints.Function;
import flash.display.*;
import openfl.events.MouseEvent;
import openfl.utils.Function;
class Main extends Sprite
{
public var oldListener:MouseEvent->String ;
public function new() {
super();
//a = new Float->String();
oldListener = function(e:MouseEvent): String
{
trace("abc");
return "";
};
stage.addEventListener( MouseEvent.CLICK, oldListener);
var newListener:MouseEvent->String = function(e:MouseEvent): String
{
trace("def");
return "";
};
changeListener(newListener) ;
}
function changeListener(param:MouseEvent->String )
{
oldListener = param ;
}
}
If you see, in the above case I will always get “abc” , as the listener function is assigned ( by value ) instead of an instance being shared ( by reference).
you can try to stage.removeEventListener(MouseEvent.CLICK, oldListener) (if oldListener is set) then stage.addEventListener(MouseEvent.CLICK, newListener).
Yes, but that is 2 more lines every time I need to change the listener. Also I need to know what is the way to do the same in Haxe. As I use it in AS3 without any problem. And what and how type “function” is used?