Type checking launched recursive in kotlin

 val cycleRunnable = Runnable {
        handler.postDelayed(cycleRunnable,100)
    }

I get the error Error: (219, 29) The type check passed a recursive task. The easiest workaround: explicitly specify the types of your ads

But its exact java version has no error

private final Runnable cycleRunnable = new Runnable() {
        public void run() {
                handler.postDelayed(cycleRunnable, POST_DELAY);
        }
    };
+4
source share
3 answers

Kotlin prohibits the use of a variable or property inside its own initializer.

You can use the expression for implementation the Runnablesame way as in Java:

val cycleRunnable = object : Runnable {
    override fun run() {
        handler.postDelayed(this, 100)
    }
}

Another way to do this is to use some function that will return Runnableand use cycleRunnableinside the lambda passed to it, for example:

val cycleRunnable: Runnable = run {
    Runnable {
        println(cycleRunnable)
    }
}

. , :

: utils kotlin-fun :

val cycleRunnable: Runnable = selfReference {
    Runnable {
        handler.postDelayed(self, 100)
    }
}
+8

? , , -, cycleRunnable .

val cycleRunnable = object:Runnable {
    override fun run() {
        handler.postDelayed(this, 100)
    }
}

lazy , cycleRunnable :

val cycleRunnable: Runnable by lazy {
    Runnable { handler.postDelayed(cycleRunnable, 100) }
}
+2

, , , Unit aka { ... }

Kotlin :

fun doSomethingElse() = doSomething()
fun doSomething() { }

, :

fun recursiveFunction(int: Int) =
    when (int) {
        1 -> { }
        else -> recursiveFunction()
    }

:

fun recursiveFunction(int: Int) {
    when (int) {
        1 -> { }
        else -> recursiveFunction()
    }
}
+1
source

Source: https://habr.com/ru/post/1682775/


All Articles