Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't use continue in let or run

Tags:

kotlin

Why it is not allowed to continue from let function?

This code:

fun foo(elements: List<String?>) {
    for (element in elements) {
        element?.let {
            continue  // error: 'break' or 'continue' jumps across a function or a class boundary
        }
    }
}

And even this code:

fun foo(elements: List<String?>) {
    loop@ for (element in elements) {
        element?.let {
            continue@loop  // error: 'break' or 'continue' jumps across a function or a class boundary
        }
    }
}

Does not compile with error:

'break' or 'continue' jumps across a function or a class boundary


I know that in this particular case I can use filterNotNull or manual check with smart cast, but my question is why it is not allowed to use continue here?


Please vote for this feature here: https://youtrack.jetbrains.com/issue/KT-1436

like image 792
diralik Avatar asked Jun 03 '20 20:06

diralik


People also ask

What does continue not properly in loop mean?

The continue keyword moves the order of a program onto the next iteration in a loop. If you use a continue statement outside of a for loop or a while loop, the SyntaxError: continue not properly in loop error will be raised. This guide explores what this error means and why you may encounter it.

What is the use of continue statement in C++?

Note: The continue statement can be used with any other loop also like while loop in a similar way as it is used with for loop above. Given a number n, print triangular pattern.

Is it better to break the loop and continue the conditions?

Generally speaking the conditions for your loops should be contained purely within those loop conditions not littered throughout the body. However there are some situations where break and continue can help readability. break moreso than continue I might add :D

What is the use of continue in Python while loop?

Python while loop exception continue 4. Python nested while loop continue 5. Python while true loop continue 6. While loop continue Python example 7. Python try except continue while loop 8. Python while loop break and continue Let us see how to use the continue statement in the While loop in Python. Continue is used to skip the part of the loop.


1 Answers

These would be called "non-local" breaks and continues. According to the documentation:

break and continue are not yet available in inlined lambdas, but we are planning to support them too.

Using a bare (e.g. non-local) return inside a lambda is only supported if it is an inlined lambda (because otherwise it doesn't have awareness of the context it is called from). So break and continue should be able to be supported. I don't know the reason for the functionality to be delayed.

Note, there are work-arounds for both of them by run either inside or outside the loop, and taking advantage of the fact that at least non-local returns are supported for inline functions.

fun foo(elements: List<String?>) {
    run {
        for (element in elements) {
            element?.let {
                println("Non-null value found in list.")
                return@run // breaks the loop
            }
        }
    }
    println("Finished checking list")
}

fun bar(elements: List<String?>) {
    for (element in elements) {
        run {
            element?.let {
                return@run // continues the loop
            }
            println("Element is a null value.")
        }
    }
}
like image 157
Tenfour04 Avatar answered Oct 21 '22 14:10

Tenfour04