I wanted to understand what is actual use case when we would want to add crossinline
keyword to a lambda
I have following code where I added crossinline. I understand that crossinline will force any of the calling method to force it to local-return only.
fun main() {
print("Main Call")
higherOrderFunction {
print("Step inside lambda")
return@higherOrderFunction
}
print("Main Call After")
}
inline fun higherOrderFunction(crossinline lambda: () -> Unit) {
print("Inside Higher Order")
lambda()
}
What I don't understand is the actual use case of using it. When does it becomes absolutely necessary to use crossinline? What's the advantage of enforcing the non-local return. What is that use case when we can't rely on developer to manually local-return. Asking this because if I remove the crossinline in the same code, it works fine even then. I checked the decompiled code and I see that its same for both the code.
fun main() {
print("Main Call")
higherOrderFunction {
print("Step inside lambda")
return@higherOrderFunction
}
print("Main Call After")
}
inline fun higherOrderFunction(lambda: () -> Unit) {
print("Inside Higher Order")
lambda()
}