The position of the @ sign in Kotlin, denoting the receiver with `this`

I am new to Kotlin. I am interested to know the difference of this marked in Kotlin with the prefix @ or postfix @ .

I just saw code that writes SignInActivity@this , which seems to work just like this@SignInActivity .

Are these two exactly the same? If not, what is the difference between the two?

I tried to do some research in the form of *@this , but I could not find any links to it. All I could find was this white paper that demonstrates this@ * . It would be nice if someone can share me with the right link that I have to go to.

+5
source share
2 answers

SignInActivity@ this is just another expression for this , with the functionality of defining an unnecessary label called SignInActivity (which has nothing to do with the actual class name) for this .

According to the Kotlin Grammar Documentation :

 labelReference (used by atomicExpression, jump) : "@" ++ LabelName ; labelDefinition (used by prefixUnaryOperation, annotatedLambda) : LabelName ++ "@" ; 

hello@ is just a label named "hello" (for Returns and Transitions ),

whereas @hello is a reference for a marked loop or block.

These combined expressions can be used as shown below:

 loop@ for (i in 1..100) { for (j in 1..100) { if (...) break@loop //jump to loop@ } } 
+5
source

SignInActivity @ this means SignInActivity.this (Java) this @SignInActivity means using the SignInActivity context instead of the local context (usually in closure).

+1
source

Source: https://habr.com/ru/post/1273835/


All Articles