s3rius
02/18/2024, 11:21 AMclass FunInterfaceTest {
fun interface FunInterface {
suspend operator fun invoke() // <- suspend
}
fun func(f: FunInterface) = Unit
@Test
fun `this fails on k2`() {
val lambda: () -> Unit = { } // <- no suspend
func(f = lambda)
}
@Test
fun `this always works`() {
val lambda: suspend () -> Unit = { } // <- suspend
func(f = lambda)
}
}
With this first test throwing the following runtime error:
java.lang.ClassCastException: class <class>$lambda$1 cannot be cast to class kotlin.jvm.functions.Function1 (<class>$lambda$1 and kotlin.jvm.functions.Function1 are in unnamed module of loader 'app')
In short: I'm calling a function f
that expects a FunInterface
. The fun interface's function is suspending.
On 1.9.22, I can use a lambda of type () -> Unit
or of type suspend () -> Unit
to call f
.
On K2 beta 4, I can only use a lambda of type suspend () -> Unit
to call f
.
I have a sneaking suspicion that using the non-suspend variant on 1.9.22 worked just by coincidence, and that I've been doing something that isn't actually allowed by the language.
Still, it's a change of behavior compared to 1.9. And I would have assumed that the compile-time type checking should throw an error during compilation if this were disallowed. Is this worth reporting?mglukhikh
02/18/2024, 11:46 AMs3rius
02/18/2024, 12:08 PMkirillrakhman
02/19/2024, 10:04 AM