I am having the following error with this piece of code, which makes no sense to me:
fun spawnWorker(): Runnable {
return Runnable {
LOG.info("I am a potato!")
return
}
}
My IDE says this to me:
But the Runnable interface says otherwise:
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
What is the reason why I can't have a return there, but without any return it compiles fine:
fun spawnWorker(): Runnable {
return Runnable {
LOG.info("I am a potato!")
}
}
A plain
return
returns from the nearest enclosing function or anonymous function. In your example, the return is non-local and returns fromspawnWorker
and not from theRunnable
SAM adapter. For a local return, use the labeled version: