🐦 Kotlin
Advanced
What is the crossinline modifier in Kotlin?
Answer
When a function is inline, lambdas passed to it support non-local returns — you can return from the outer function inside the lambda. However, if the lambda is passed to another execution context (like a Runnable, a callback, or stored for later execution), non-local returns are not safe. The crossinline modifier on a lambda parameter of an inline function prohibits non-local returns from that lambda, allowing it to be used in contexts that do not permit non-local returns, while still inlining the lambda's body for performance. It is a compromise between full inlining and the safety of noinline.
Previous
What is a function type with receiver in Kotlin?
Next
What is the noinline modifier in Kotlin?