Skip to content

Instantly share code, notes, and snippets.

@kLabz
Last active February 13, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kLabz/90891dd084234680019483031920e9f4 to your computer and use it in GitHub Desktop.
Save kLabz/90891dd084234680019483031920e9f4 to your computer and use it in GitHub Desktop.
Strictly typed event handlers
class Test {
static function main() {
addEventListener(MouseEventType, function(e:MouseEvent) {});
addEventListener(KeyboardEventType, function(e:KeyboardEvent) {});
// addEventListener(MouseEventType, function(e:KeyboardEvent) {}); // error: MouseEvent should be KeyboardEvent
}
static function addEventListener<T:EventBase>(
eventType:EventType<T>,
handler:T->Void
):Void {trace(eventType);}
}
class EventBase {}
class MouseEvent extends EventBase {}
class KeyboardEvent extends EventBase {}
enum abstract EventType<T:EventBase>(String) to String {
var MouseEventType:EventType<MouseEvent> = "mouseevent";
var KeyboardEventType:EventType<KeyboardEvent> = "keyboardevent";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment