Returns and Jumps

Kotlin has three structural jump expressions:

  • return. By default returns from the nearest enclosing function or anonymous function.
  • break. Terminates the nearest enclosing loop.
  • continue. Proceeds to the next step of the nearest enclosing loop.

All of these expressions can be used as part of larger expressions:

  1. val s = person.name ?: return

The type of these expressions is the Nothing type.

Break and Continue Labels

Any expression in Kotlin may be marked with a label. Labels have the form of an identifier followed by the @ sign, for example: abc@, fooBar@ are valid labels (see the grammar). To label an expression, we just put a label in front of it

  1. loop@ for (i in 1..100) {
  2. // ...
  3. }

Now, we can qualify a break or a continue with a label:

  1. loop@ for (i in 1..100) {
  2. for (j in 1..100) {
  3. if (...) break@loop
  4. }
  5. }

A break qualified with a label jumps to the execution point right after the loop marked with that label. A continue proceeds to the next iteration of that loop.

Return at Labels

With function literals, local functions and object expression, functions can be nested in Kotlin. Qualified returns allow us to return from an outer function. The most important use case is returning from a lambda expression. Recall that when we write this:

  1. //sampleStart
  2. fun foo() {
  3. listOf(1, 2, 3, 4, 5).forEach {
  4. if (it == 3) return // non-local return directly to the caller of foo()
  5. print(it)
  6. }
  7. println("this point is unreachable")
  8. }
  9. //sampleEnd
  10. fun main() {
  11. foo()
  12. }

The return-expression returns from the nearest enclosing function, i.e. foo. (Note that such non-local returns are supported only for lambda expressions passed to inline functions.) If we need to return from a lambda expression, we have to label it and qualify the return:

  1. //sampleStart
  2. fun foo() {
  3. listOf(1, 2, 3, 4, 5).forEach lit@{
  4. if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop
  5. print(it)
  6. }
  7. print(" done with explicit label")
  8. }
  9. //sampleEnd
  10. fun main() {
  11. foo()
  12. }

Now, it returns only from the lambda expression. Oftentimes it is more convenient to use implicit labels: such a label has the same name as the function to which the lambda is passed.

  1. //sampleStart
  2. fun foo() {
  3. listOf(1, 2, 3, 4, 5).forEach {
  4. if (it == 3) return@forEach // local return to the caller of the lambda, i.e. the forEach loop
  5. print(it)
  6. }
  7. print(" done with implicit label")
  8. }
  9. //sampleEnd
  10. fun main() {
  11. foo()
  12. }

Alternatively, we can replace the lambda expression with an anonymous function. A return statement in an anonymous function will return from the anonymous function itself.

  1. //sampleStart
  2. fun foo() {
  3. listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
  4. if (value == 3) return // local return to the caller of the anonymous fun, i.e. the forEach loop
  5. print(value)
  6. })
  7. print(" done with anonymous function")
  8. }
  9. //sampleEnd
  10. fun main() {
  11. foo()
  12. }

Note that the use of local returns in previous three examples is similar to the use of continue in regular loops. There is no direct equivalent for break, but it can be simulated by adding another nesting lambda and non-locally returning from it:

  1. //sampleStart
  2. fun foo() {
  3. run loop@{
  4. listOf(1, 2, 3, 4, 5).forEach {
  5. if (it == 3) return@loop // non-local return from the lambda passed to run
  6. print(it)
  7. }
  8. }
  9. print(" done with nested loop")
  10. }
  11. //sampleEnd
  12. fun main() {
  13. foo()
  14. }

When returning a value, the parser gives preference to the qualified return, i.e.

  1. return@a 1

means “return 1 at label @a“ and not “return a labeled expression (@a 1)“.