Kotlin object expression not working as expected -


i going through kotlin exercises on github (see link below).

i don't quite understand following code.

specifically

  1. where mouse listener created?

  2. how mouselistener invoked? invoked 4 times. didn't see syntax before. great if point documentation.

 // code on github , working val result = task10 { mouselistener ->     mouselistener.mouseclicked(mouseevent)     mouselistener.mouseclicked(mouseevent)     mouselistener.mouseclicked(mouseevent)     mouselistener.mouseclicked(mouseevent) } 

i thinking code should written following. if ran, no event gets triggered.

// not working val result = task10 {     mouselistener  ->     {         mouselistener.mouseclicked(mouseevent)         mouselistener.mouseclicked(mouseevent)         mouselistener.mouseclicked(mouseevent)         mouselistener.mouseclicked(mouseevent)     } } 

the full declaration of method is:

fun task10(handlemouse: (mouselistener) -> unit): int {     var mouseclicks = 0     handlemouse(todotask10())     return mouseclicks  } 

full source code here , here.

thank in advance.

in case curly braces means lambda declaration (when it's not part of other declaration). i.e. second example pass task10 lambda returns lambda.

you can find more information in the reference

note: should warning on inner lambda ide , compiler.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -