What is a Label for Kotlin?

7

In an example of loops in Kotlin documentation , we have some code with following excerpt:

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

As I read in the documentation, it seems to be a Label and they can be placed in any type of expression, as 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 any language I've programmed to date, there was a need to do something similar in a code snippet about a repeat structure.

I did not understand the function of this loop@ at the beginning and the @loop in case of the examples presented in the documentation (mainly that of% of example%).

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

  • Does it have to do with GOTO? Or is it just something like for of C #?

asked by anonymous 08.08.2017 / 16:45

3 answers

6

It's a goto form, although some do not like using that word :). Because people are prejudiced by the word, they can only use break , continue and return . which people accept better, but the mechanism is the same. Of course you have some restrictions on how you can use it, which is a good thing to avoid code madness.

The label is the address in the code where the deviation should go.

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

It is rare to be necessary, but it is good to have the recourse for such cases. Besides that if the language is used as target of another it is important to have this type of characteristic to better generate codes.

C # prefers% with% of itself, Java has only goto nominated.

    
08.08.2017 / 16:50
5

Used to name the code block. It's like a goto .

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

    
08.08.2017 / 16:50
2

It is necessary to understand that, in the exemplified code, the label is intended to mark the beginning of the expression, in this case 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, break@loop is used. With @loop refers to the first for, then it will be stopped when it reaches the condition of if which is within the second for .

If the label was not used, break would stop only the second for , which might not be the desired behavior. If this were not the case, we would probably have to take a big turn to get the first for from the second.

  

What is the purpose of these Labels in an expression?

As stated in other answers, it is similar to the GOTO that exists in some languages.

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

  

... Or is it just something like the region ... Csharp endregion?

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

  

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

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

    
08.08.2017 / 18:27