Search code examples
javalambdasyntaxkotlinkotlin-interop

kotlin: syntax for 2 lambda parameter


I'm new to kotlin. I have a java class with 2 overloaded methods. One accepts one function, the other one accepts two

mapToEntry(Function<? super T, ? extends V> valueMapper)

and

mapToEntry(Function<? super T, ? extends K> keyMapper, 
           Function<? super T, ? extends V> valueMapper)

nowm in kotlin, i'm trying to call the the version with 2 parameters (as in java):

myClass.mapToEntry(r -> r, r -> r)

but i get compilation error.

Kotlin: Unexpected tokens (use ';' to separate expressions on the same line)

what's the correct syntax?


Solution

  • In Kotlin, lambda expressions are always surrounded by curly braces, so it's

    myClass.mapToEntry({ r -> r }, { r -> r })
    

    See: Lambda Expression Syntax