https://kotlinlang.org logo
Title
s

snowe

02/19/2019, 4:48 AM
why does a non-local return from an inline function, have to be passed through in a lambda? e.g.
returnFromInline { return }
    inline fun returnFromInline(ret: () -> Unit) {
            ret()
    }
but not
returnFromInline()
    inline fun returnFromInline() {
            return
    }
s

SOFe

02/19/2019, 6:27 AM
Java interop
If you call an inline function from Java, Kotlin would generate a concrete (non-inline) method of the function
However it is not possible to return on behalf of the caller from another function (unless you throw an exception, which is another business)
also, the inline function context does not know the return type of the calling context, so it cannot determine what type to return
so in terms of feasibility, your syntax is not possible.
why would you want to do that anyway? forced returns are mostly not portable.
for example, in some cases, you might want to break from a loop rather than returning.
Your suggestion is basically like a
return@outer
syntax, which still heavily depends on the caller's return type. I don't think there is anything in the Kotlin language right now that allows you to restrict a calling context's function's return type.
d

diesieben07

02/19/2019, 7:28 AM
It's not really Java interop, Java cannot use (most) inline functions at all. The reason here is, as far as I can tell, that a called function (inline or not) makes it's caller "magically" return something is too much "spooky action at a distance" to be feasible for Kotlin.
2
s

sahil Lone

04/24/2019, 6:27 PM
@diesieben07 You are right its not java interoperability, Nut the reason is because inline functions are not a function call like involving stacks and all, but rather reduced to lines of code when compiled, Thats the reason , to capture the state of variable a lambda is needed which is acatially a tyoe of
FunctionX