Kotlin prohibits usage of a variable or a property inside its own initializer.
You can use an object expression to implement Runnable
in the same way as in Java:
val cycleRunnable = object : Runnable {
override fun run() {
handler.postDelayed(this, 100)
}
}
Another way to do that is to use some function that will return the Runnable
and to use cycleRunnable
inside the lambda passed to it, e.g.:
val cycleRunnable: Runnable = run {
Runnable {
println(cycleRunnable)
}
}
Or see a workaround that allows a variable to be used inside its own initializer through a self reference:
This code will not work out of the box: you need to add the utils from the link above or use the kotlin-fun
library:
val cycleRunnable: Runnable = selfReference {
Runnable {
handler.postDelayed(self, 100)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…