Hi, I need some help to understand what's going on...
# announcements
a
Hi, I need some help to understand what's going on with a lambda call to an interface. I have the following interface in Java:
Copy code
public interface ThreadExecutor {
    void execute(Runnable runnable);
}
Which I can call from Kotlin this way:
Copy code
threadExecutor.execute { //code }
But, If I convert the interface to Kotlin, then I get an error in those calls, saying that they expect a Runnable but the return is Unit.
Copy code
interface ThreadExecutor {
    fun execute(runnable: Runnable)
}
And I have to explicitly create the Runnable object.
Copy code
threadExecutor.execute(Runnable { /* Code */ })
Would be great to understand why it happens, but also any idea on how to better solve it without having to create the Runnable in every call?
t
it should be fixed in kotlin 1.4 with functional interface.
Copy code
fun interface ThreadExecutor {
    fun execute(runnable: Runnable)
}
the magic work is
fun
😄
r
Presumably you still need use
ThreadExecutor
from Java, hence continuing to take a
Runnable
rather than a
() -> Unit
?
a
Yes I need to define the argument as Runnable
👍 1
I'll check that Marco. Thanks both for the answers
k
Making
ThreadExecutor
a functional interface does not change anything in this example, because the SAM conversion is required for
Runnable
and not
ThreadExecutor
. However upgrading to 1.4 will solve this issue, as it widens the capabilities of SAM conversions in general.
t
🤦 totally right Iven, I cannot even blame the morning, it was already 10ish here.
a
So, upgrading to Kotlin 1.4 will solve it without anything else? That would be great, thanks!