class EventHandler<T> {
private var handlers: Array<T> = [];
public function new () {}
@:op(A + B)
public function add(func: T) {
this.handlers.push(func);
}
}
class Main {
public static function main() {
var h = new EventHandler<String->Void>();
h += function(str: String) {
trace(str);
}
}
}
Then I receive an error, meaning that I cannot perform a += operator on different types:
Classes don’t support operator overloading in Haxe, only abstracts do. The usual workaround is to rename the class to something like EventHandlerImpl and wrap it an abstract.
import haxe.Constraints.Function;
using Test;
class Test {
static function main() {
var e = new EventHandler<String->Void>();
e += function(s) trace('$s, world');
e += function(s) trace('$s, haxe');
e.dispatch('Hello!');
}
}
abstract EventHandler<T:Function>(Array<T>) {
var handlers(get,never):Array<T>;
inline function get_handlers() return this;
public inline function new() {
this = [];
}
@:op(a += b) inline function add(fn:T) {
this.push(fn);
}
}
class EventHandlerDispatcher1 {
public static inline function dispatch<T>(e:EventHandler<T->Void>, arg:T) {
for(fn in @:privateAccess e.handlers) {
fn(arg);
}
}
}
class EventHandlerDispatcher2 {
public static inline function dispatch<T1,T2>(e:EventHandler<T1->T2->Void>, arg1:T1, arg2:T2) {
for(fn in @:privateAccess e.handlers) {
fn(arg1, arg2);
}
}
}
//... for whatever amount of arguments
Thanks @ReallyUniqueName, this really helped me a lot! I’ve also added a -= operator based on your implementation
There’s one caveat though, when defining EventHandler on another file, the compiler cannot understand the dispatch() method. Do you know how this can be fixed?
Test.hx:8: characters 4-14 : EventHandler Void> has no field dispatch