Vlad
01/14/2025, 4:54 PMhighOrder()
effectively do exactly the same:
object SomeClass {
fun doSomething(id: Int) {}
}
fun highOrder(
a: (something: Int) -> Unit
) {
a(35)
}
fun main() {
// Usage 1
highOrder(
// Passing Lambda expression (could be trailing)
a = { something ->
SomeClass.doSomething(id = something)
}
)
// Usage 2
highOrder(
// Passing Method reference.
a = SomeClass::doSomething
)
}
Can we say that Usage 2 is lambda or it will be technically incorrect?
Will be there different output from the compiler for the usages? (I need to learn how to see the complier output 0\)Klitos Kyriacou
01/14/2025, 5:15 PMhighOrder
function. A lambda expression is one way to create such a function-type parameter, it's not a type itself, but just a syntax. Even though the Kotlin compiler (at least the current versions) produces different bytecodes for these two usages, they're equivalent and their performance is almost the same.ephemient
01/14/2025, 5:16 PM{ ... }
is slightly different than the object constructed for a function reference ::
, as the latter is also a KCallable with ==
and .name
etc.ephemient
01/14/2025, 5:18 PMinline fun highOrder(
a: (something: Int) -> Unit
)
then the bytecode would be the same in both cases, as it skips creating the lambda objectVlad
01/14/2025, 5:22 PMephemient
01/14/2025, 5:25 PM