Search code examples
luaspecial-charactersgoto

What is the purpose of double colons in Lua?


I have been aware that version 5.3 for Lua had come out not too long ago but hadn't had a reason to visit the documentation online until now. I may be wrong, but I do not believe to remember the usage of the double-colons :: like it is used so abundantly there.

I see that it is considered a "special token" like others are (greater than, less than, asterisks, etc) but I know what those are for.

What is the purpose of using them in Lua?


Solution

  • :: is only used for one thing in Lua *:

    Declaring labels for jumping with goto.

    goto label
    ::label::
    

    The goto statement transfers the program control to a label. For syntactical reasons, labels in Lua are considered statements too:

    stat ::= goto Name
    stat ::= label
    label ::= ‘::’ Name ‘::’
    

    A label is visible in the entire block where it is defined, except inside nested blocks where a label with the same name is defined and inside nested functions. A goto may jump to any visible label as long as it does not enter into the scope of a local variable.

    Labels and empty statements are called void statements, as they perform no actions.

    * I don't consider extensive use with extended BNF in the documentation use in Lua itself.