Convert Java to Kotlin ERROR


#1

when i convert my Java code to Kotlin,i got a error,any one can help? thanks~


#2

I’d see if it’s any better if you change the first line to:

private val playTimeDelayRun: Runnable get() = Runnable {

#3

If i define Runnable like this:

private val playTimeDelayRun: Runnable get() = Runnable {

Is not every call to get () will re-create a new Runnable return?


#4

If it compiles then, yes, a new instance of Runnable will be created on each invocation.

However, if you’re converting from a Java ‘get’ accessor function, then that may be what would have happened anyway.

Also, there’s a danger that if the same instance were used in the recursive call then the code might be circular.


#5

Maybe I should have this to define:

private lateinit var playTimeDelayRun: Runnable

init{
playTimeDelayRun = Runnable{…}
}

But it’s not simple to write


#6

Something like that might work but can you post your original Java code so I can try and work out what the closest Kotlin equivalent would be.