What’s a Kotlin label for?

Asked

Viewed 213 times

8

In an example of loops in kotlin documentation, we have some codes with the following excerpt:

loop@ for (i in 1..100) {
    for (j in 1..100) {
        if (...) break@loop
    }
}

From what I read in the documentation, it appears to be a Label and that they can be placed in any type of expression, according to the text:

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

But in no language that I programmed until today was there a need to do something similar in a piece of code referring to a repeating structure.

I don’t understand the function of this loop@ at the beginning and the @loop in the case of the examples provided in the documentation (in particular for of the example).

  • What is the purpose of these Labels in an expression?

  • Is it about GOTO? Or is it something just like region...endregion of the C#?

  • Why at the beginning of the code the @ is after the word loop and then it’s at the beginning?

  • someone with better English than mine could translate the excerpt of the documentation to be attached to the question?

3 answers

7


Is a form of drop, although some do not like to use that word :). As people have prejudice with the word, can only use with break, continue and return that people accept better, but the mechanism is the same. Of course there are some restrictions on how you can use it, which is a good thing to avoid crazy code.

The label is the address in the code to which the deviation should go.

In your example it would be quite complicated to make a logic that breaks the way you want, would have to create a flag that is never a good thing to do, introduces unnecessary state to accompany.

It’s rare to be needed, but it’s good to have the appeal for cases like this. In addition, if the language is used as another target it is important to have this type of feature to better generate code.

C# preferred the goto even, Java has only the break nominee.

5

It’s for naming the code block. It’s like a goto.

Your example is good to illustrate, if the condition of the if the for external will be stopped. If there was no label specifying it, would be the for intern that would stop.

2

It is necessary to understand that, in the exemplified code, the label aims to mark the beginning of the expression, in the case of the for (i in 1..100).

The advantage of this is that it is possible to stop the first loop within the second loop. That is, you can reference the first scope from the second.

A small sketch of how this works could be shown like this:

  parent:
    children:
        if (condition)
            parent.break()

In the example of your question, the break@loop. With the @loop refers to the first is, then it will be stopped when it reaches the condition of if that is within the second for.

If the label was not used, breakwould stop only the second for, which might not have been the desired behavior. Otherwise, we’d probably have to go a long way to stop the first for from the second.

What is the purpose of these Labels in an expression?

As has been said in other responses, it is similar to GOTO that exists in some languages.

To Label is like a starting point. When it is referenced, you are referenced to the point at which it was defined.

... Or is it something just like the Csharp’s Region...endregion?

There is no connection to features similar to region or endregion of C#, since region is intended only to demarcate a code section to facilitate the reduction of a section. In this case, region does not affect the execution of the code, since the Label affects.

Why at the beginning of the code the @ is at the end of the word loop and then is at the beginning?

The arroba at the end of the expression indicates that you are declaring the Label for that chunk of code. Already when it is referenced it is used at the beginning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.