https://kotlinlang.org logo
Title
m

McEna

10/12/2018, 8:11 PM
Hey guys, I've been searching but I can't find a clear answer. Can inline functions be recursive? I understand they can't, but I'd like to be sure
d

Dominaezzz

10/12/2018, 8:21 PM
Just tried it. They can't be recursive.
You could always pass in a lamdba and call the function inside the lamdba and you'll get inlined recursion.
e

elizarov

10/12/2018, 8:26 PM
:troll:
j

jw

10/12/2018, 8:29 PM
tailrec maybe?
e

elizarov

10/12/2018, 8:31 PM
It is not allowed either
i

Icaro Temponi

10/12/2018, 8:36 PM
the lambda copy pasting seems to be a good alternative, considering that inline functions are just copy pasting the function body to the call site, so no recursion because there's no actual function at runtime, right?
s

Sam

10/12/2018, 10:26 PM
Tried this to see what happens, it gets through compilation check (inline & recursion)
inline fun testInlineRecursion( value : Int, block : ( Int ) -> Unit ) {

    if( value == 10 ) return
    println( "Value is $value" )
    block( value + 1 )

}

val relayToRecursiveFunLambda : ( Int ) -> Unit  = { value -> testInlineRecursion( value, ::relayToRecursiveFunLambda.get() ) }

fun main( argv : Array<String> ) {

    testInlineRecursion(1) {
        testInlineRecursion(it, relayToRecursiveFunLambda)
    }
}
even though testInlineRecursion is inlined, i guess relayToRecursiveFunLambda still goes through the recrusion
Thx to @gcx11 and @marstran to get lambda reference within a lambda
m

McEna

10/13/2018, 2:02 AM
Thanks folks! I'll give it a try