Hey guys, I've been searching but I can't find a c...
# announcements
m
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
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
🧌
j
tailrec maybe?
e
It is not allowed either
i
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
Tried this to see what happens, it gets through compilation check (inline & recursion)
Copy code
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
Thanks folks! I'll give it a try