It is indeed a label and is especially useful in that example as it labels an anonymous function. You use the label for qualifying references (like this
).
In the following example the lambda defines an inner method nested
which may want to access the this
from the funLit
. Since it is anonymous we need to label it, lambda
is an arbitrary identifier.
fun main() {
val funLit = lambda@ fun String.() {
println("this: " + this)
println("this@lambda: " + this@lambda)
fun String.nested() {
println("this in String.nested(): " + this)
println("this@nested in String.nested(): " + this@nested)
println("this@lambda in String.nested(): " + this@lambda)
}
"nested".nested()
}
"funLit".funLit()
}
Running it shows very clearly what this
is being referred to with the qualifier.
this: funLit
this@lambda: funLit
this in String.nested(): nested
this@nested in String.nested(): nested
this@lambda in String.nested(): funLit
Here is a playground link: https://pl.kotl.in/SJrlUs6LE