I have some kotlin code similar to this:
Thread {
...
Thread {
...
return@Thread
...
}.start()
...
}.start()
Now I get the following warning:
I understand that kotlin is confused as to which Thread I want to return at this point, whether it's the outer or the inner thread. But I'm not sure how to tell it and Android Studio doesn't help much either, as it only suggests editing the options for this warning:
I have tried naming the thread and thought maybe kotlin is smart enough to check for that, but I guess this might not be possible, because the return label is probably not interpreted on runtime.
I realize that I can just export the inner thread into a function and thereby have the threads not interfere with each other, like this:
Thread {
...
startInnerThread()
...
}.start()
fun startInnerThread() {
Thread {
...
return@Thread
...
}.start()
}
But I would like to know if it's somehow possible to change the label @Thread
for one of them instead.
Yea, you can do this by labelling the function to return from like so:
Thread {
Thread Foo@ {
return@Foo
}.start()
}.start()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With