tail-call optimization seems to not be implemented...
# compiler
y
tail-call optimization seems to not be implemented for suspend lambdas on JVM. Is that expected? I'll provide sample later
e
do you mean
tailrec suspend
?
y
No no, tail call optimization, as in if the last call is a suspend function, the parent continuation is passed to it instead of the current one. This causes a significant difference in the bytecode between e.g.
{ foo() }
and
::foo
. The first creates a pointless state machine, the second doesn't
e
ah that's not what I think of at all when it comes to TCO
good question. that's how it should be working in regular functions, missing it in lambdas seems like an oversight
I expect that a callable object
Copy code
object : suspend () -> Unit {
    override suspend fun invoke() {
        foo()
    }
}
would do the right thing?
y
It does, but they're not allowed on JS. A workaround I've done is:
Copy code
suspend fun myFoo() = foo(arg1)
bar(::myFoo)
Which does the right thing
e
hmm. I'd ask if
Copy code
bar(suspend fun() = foo(arg1))
would be a workaround but it seems the compiler doesn't work with those either (https://youtrack.jetbrains.com/issue/KT-62021)