Biswanath
Biswanath

Reputation: 9185

How is the "Any" class works with the "this" variable?

Please refer this, https://github.com/scala/scala/blob/v2.12.0-M1/src/library-aux/scala/Any.scala

The abstract class Any uses "this" self reference for equality test. As far understand "this" is not anything special is scala. How is the "this" value handled in "Any" ?

Upvotes: 2

Views: 78

Answers (2)

Forketyfork
Forketyfork

Reputation: 7810

this is special in Scala. First of all, according to lexical syntax section of the language spec, this is a reserved keyword. See also the 6.5 This and Super part of the spec that precisely defines the semantics of this keyword:

The expression this can appear in the statement part of a template or compound type. It stands for the object being defined by the innermost template or compound type enclosing the reference. If this is a compound type, the type of this is that compound type. If it is a template of a class or object definition with simple name C, the type of this is the same as the type of C.this.

So in your case of Any class, this is a reference to the actual object that the method of equality is called upon.

Upvotes: 2

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

this is special in Scala. It refers to the receiver of a message send (if you prefer Smalltalk nomenclature) or the object that the currently executing method was called on (if you prefer C++ nomenclature).

It is equivalent to the self keyword in Smalltalk, Self, Newspeak, Ruby, Fancy, Object Pascal, Objective-C, Swift, and their relatives or the this keyword in Java, C#, VB.NET, ECMAScript, C++, D, PHP, ECMAScript and their relatives or the Me keyword in Visual Basic or the Current keyword in Eiffel.

Upvotes: 2

Related Questions