Type checking has run into a recursive in kotlin

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

I am getting error Error:(219, 29) Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly

But its exact java version doesn't have any error

private final Runnable cycleRunnable = new Runnable() {
        public void run() {
                handler.postDelayed(cycleRunnable, POST_DELAY);
        }
    };
4 Answers

Show value type explicitly like below:

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

This solution solves most of the problems with recursive typechecking problem.

If you are receiving error Variable must be initialized then you have to declare variable first, and then initialize it with runnable like below:

lateinit var cycleRunnable:Runnable

//in constructor or somewhere else:
cycleRunnable = Runnable {
        handler.postDelayed(cycleRunnable,100)
    }
Related