Saturday, 15 June 2013

events - Java FX-8: Why is EventType generic? -



events - Java FX-8: Why is EventType generic? -

the scheme of eventtype highly confusing in javafx. think reason designers decided create eventtype class generic.

is there practical utilize or obscure form of ensuring type safety ?

i imagine classes made generic if generic type returned somewhere calling class. otherwise what's point ?

there event classes, i.e. subclasses of event, define particular methods class. e.g. mouseevent defines various methods can used query coordinates of mouse when event occurred (getx(), gety(), getscenex(), getscreenx() etc); scrollevent defines methods query amount , direction of scroll: getdeltax(), getdeltay(), etc).

eventtype more fine-grained object specifies happened. constants of eventtype class include mouse_clicked, mouse_pressed, key_typed, scroll_started, etc. each of these values associated particular event subclass.

the event registration method addeventhandler(...). method takes 2 parameters: eventtype (which type of event hear for), , eventhandler. eventhandler generic obvious reasons: defining eventhandler<t extends event> define handle(t event) method takes appropriate event object, can query in type-specific way. (e.g. eventhandler<mouseevent> has handle(mouseevent event) method, can query coordinates of mouse, etc).

now, makes sense register eventhandler<t> eventtype associated event subclass t. furthermore, it's desirable allow compiler enforce this. making eventtype class generic: eventtype<t extends event>, allow compiler enforce rule. mouse_clicked not instance of eventtype, it's instance of eventtype<mouseevent>. since signature of addeventhandler(...) method addeventhandler(eventtype<t>, eventhandler<t>), compiler enforces handler "for same class of event" event type.

when combine using lambdas (or suppose type inference, in particular), becomes quite powerful:

node.addeventhandler(mouse_clicked, e -> {...});

because mouse_clicked eventtype<mouseevent>, compiler able infer event handler defined lambda eventhandler<mouseevent>, , e mouseevent. compiler can infer

node.addeventhandler(mouse_clicked, e -> system.out.println(e.getx())); node.addeventhandler(key_typed, e -> system.out.println(e.gettext()));

are legal,

node.addeventhandler(mouse_clicked, e -> system.out.println(e.gettext()));

is not. if eventtype not generic, not possible.

(the same true without lambdas, less concise syntax doesn't drive point home well. without eventtype beingness generic, there no way compiler reject

node.addeventhandler(mouse_clicked, new eventhandler<keyevent>() { @override public void handle(keyevent event) { } });

again, fact mouse_clicked has type eventtype<mouseevent> means not compile.)

technically, eventtype used "runtime-type token". it's beingness used not events interested in, type of event handler should used handle event. other examples of utilize of generics include class<t>: see this part of java tutorial brief discussion.

java events generics javafx-8

No comments:

Post a Comment