Is it possible to create singleton class when use Callback ? Example ( Try Haxe ! ) :
class Test {
static function main() {
var sm = new SignalManager();
sm.add(test);
}
static function test(arg:Int, arg1:String):Void
{
}
}
class SignalManager<Callback>
{
public static var instance(get, never) : SignalManager<Callback>;
private static var _instance : SignalManager<Callback>;
private static function get_instance() : SignalManager<Callback>
{
if (_instance == null) {
_instance = new SignalManager();
}
return _instance;
}
public function new()
{
}
public function add(listener:Callback):Void
{
trace(listener);
}
}
This return Type not found : Callback . When tried to remove from the class also get the same error for the add(...)
Type-parameters are per-instance, you cannot use them on static fields – what should the Callback type be for SignalManager.instance? If you have a specific type for your callbacks, say (arg:Int, arg1:String) -> Void like in your example, use that instead of a type-parameter Callback. I.e
public static var instance(get, never) : SignalManager<(arg:Int, arg1:String) -> Void>;
I want ot use signals or msignal library and to manage all signals via one class SignalManager.hx.
The main idea was to have a HashMap where to store event name and the second param to be the Signal ( 0,1,2) class , and fill the Hashmap from method add available in SignalManager.
But I do not know the type of params for Signal1 and Signal2 and I can’t do new Signal1<T,T>
If SignalManager works, I should have methods dispatch, remove and add where for add will accept : event name , listener function and type of signal ( with zero, one or two params) , so in add will have something like:
function add(eventName:String, listner:Callback, numParams:Int = 0) {
if ( numParams ==0) {
var signal = new new Signal0();
signal.add(listner);
signalMap[eventName] = listener;
} else if ( numParams == 1) {
var signal = new new Signal1()<T>;
signal.add(listner);
signalMap[eventName] = listener;
} else if ( numParams == 2) {
var signal = new new Signal2()<T,T>;
signal.add(listner);
signalMap[eventName] = listener;
}
}
So the idea was to call all signals ( events ) via SignalManager, but obviously it’s not a practical solution for a signal library.