Kotlin has these 2 features and I think there're no significant differences between these two regardless of :
- syntax
// lambda
val toUpper = { value: String ->
if (value.isEmpty()) "empty value"
else value.toUpperCase()
}
// anonymous func
val toUpper = fun(value: String): String {
if (value.isEmpty()) return "empty value"
else return value.toUpperCase()
}
- flexibility to use return statement on anonymous function
I'm still digesting these features and hope you guys can help me pass through it. Thanks.
A.() -> Bis same as(A) -> Bin bytecode (both areKFunction1<A, B>) but inside syntax is differentA.() -> Blets you havethisas receiver while(A) -> Blets you have variable of any name. Similarly(String) -> Stringcan be assigned to both of them, i.e. fun one is also an implementation ofKFunction1<String, String>.