this@MainActivity vs MainActivity@this in kotlin

Viewed 3415

As you may see that

this@MainActivity or MainActivity@this

is valid while starting activity in kotlin. I tried to find out answer but I did not found anything. Can anyone know about exact difference of it and which one valid ? Why it allowed both ?

2 Answers

According to Kotlin's Grammar documentation,

MainActivity@this is just a label for this with the name "MainActivity". You can set whatever label you want.

this@MainActivity is an actual reference of this from MainActivity

Hope I make it clear.

this@MainActivity - it is a reference to current MainActivity instance.

MainActivity@this - it is definition of a name of the label, i.e. MainActivity.

More info about labels.

Related