-
-
Save kLabz/90891dd084234680019483031920e9f4 to your computer and use it in GitHub Desktop.
Strictly typed event handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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