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
where mouse listener created?
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 }
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
Post a Comment