Local returns with "reversed" labels changes function logic

Hey All,

Recently started to educated myself with Kotlin.
While reading about labels and around with it, I got confused with an observed function output.
Well, this issue appeared due to the typo I originally made within my code.

The functions I used:

  1. The one that described within guide lines:
    fun foo() {
    listOf(1, 2, 3, 4, 5).forEach getout@ {
    if(it == 4) return@getout
    print(it)
    }
    print(" Done")
    }
    The output of this function is: 1 2 3 5 Done

  2. This function includes my typo:
    fun foo() {
    listOf(1, 2, 3, 4, 5).forEach getout@ {
    if(it == 4) returngetout@
    print(it)
    }
    print(" Done")
    }

This time the output was quite an opposite:
4 Done.

Furthermore, after inspecting the decompiled Java code, I found that the conditions were opposite.
For the first function it looked like this:

if(it != 4){
System.out.print(it)
}

And for the second function it was like that:

if(it == 4){
System.out.print(it)
}

So basically, as far as my understanding goes, by changing the order of “@” after return we may alter behavior.

Still my own conclusion sounds a bit silly to me. I think I missed something out. On the other hand I failed to find some sort of documentation about this. So possibly this is a bug?

Will appreciate if someone could clarify things out a bit :slight_smile:

Sincerely

returngetout@ is a label, so the second example is if(it==4) print(it)

Sweet.
Thanks for the answer.
Now I see it.

Again thank you fvasco