This (computer programming)


', ', and are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like this is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.

Object-oriented programming

In many object-oriented programming languages, this is a variable that is used in instance methods to refer to the object on which they are working. The first OO language, SIMULA 67, used this to explicitly reference the local object. C++ and languages which derive in style from it also generally use this. Smalltalk and others, such as Object Pascal, Perl, Python, Ruby, Rust, Objective-C, DataFlex and Swift, use self. Microsoft's Visual Basic uses Me.
The concept is similar in all languages: this is usually an immutable reference or pointer which refers to the current object; the current object often being the code that acts as 'parent' or 'invocant' to the property, method, sub-routine or function that contains the this keyword. After an object is properly constructed, or instantiated, this is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to make symbols within their class visible. Or alternatively, the current object referred to by this may be an independent code object that has called the function or method containing the keyword this. Such a thing happens, for example, when a JavaScript event handler attached to an HTML tag in a web page calls a function containing the keyword this stored in the global space outside the document object; in that context, this will refer to the page element within the document object, not the enclosing window object.
In some languages, for example C++, Java, and Raku this or self is a keyword, and the variable automatically exists in instance methods. In others, for example, Python, Rust, and Perl 5, the first parameter of an instance method is such a reference. It needs to be specified explicitly. In Python and Perl, the parameter need not necessarily be named this or self; it can be named freely by the programmer like any other parameter. However, by informal convention, the first parameter of an instance method in Perl or Python is named self. Rust requires the self object to be called &self or self, depending on whether the invoked function borrows the invocant, or moves it in, respectively.
Static methods in C++ or Java are not associated with instances but classes, and so cannot use this, because there is no object. In other languages, such as Ruby, Smalltalk, Objective-C, or Swift, the method is associated with a class object that is passed as this, and they are called class methods. For class methods, Python uses cls to access to the class object.

Subtleties and difficulties

When lexical scoping is used to infer this, the use of this in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses of this in this case, such as referring to instance variables hidden by local variables of the same name, or if the method wants to return a reference to the current object, i.e. this, itself.
In some compilers, pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit this pointer parameter.

Open recursion

The dispatch semantics of this, namely that method calls on this are dynamically dispatched, is known as open recursion, and means that these methods can be overridden by derived classes or objects. By contrast, direct named recursion or anonymous recursion of a function uses closed recursion, with static dispatch. For example, in the following Perl code for the factorial, the token __SUB__ is a reference to the current function:

use feature ":5.16";
sub

By contrast, in C++ the this binds to the object itself, but if the class method was declared "virtual" i.e. polymorphic in the base, it's resolved via dynamic dispatch so that derived classes can override it.

unsigned int factorial

This example is artificial since this is direct recursion, so overriding the factorial method would override this function; more natural examples are when a method in a derived class calls the same method in a base class, or in cases of mutual recursion.
The fragile base class problem has been blamed on open recursion, with the suggestion that invoking methods on this default to closed recursion rather than open recursion, only using open recursion when it is specifically requested; external calls would be dynamically dispatched as usual. The way this is solved in practice in the JDK is through a certain programmer discipline; this discipline has been formalized by C. Ruby and G. T. Leavens; it consists of the following rules:
  • No code invokes public methods on this.
  • Code that can be reused internally is encapsulated in a protected or private method; if it needs to be exposed directly to the users as well, then a wrapper public method calls the internal method.
  • The previous recommendation can be relaxed for pure methods.

    Implementations

C++

Early versions of C++ would let the this pointer be changed; by doing so a programmer could change which object a method was working on. This feature was eventually removed, and now this in C++ is an r-value.
Early versions of C++ did not include references and it has been suggested that had they been so in C++ from the beginning, this would have been a reference, not a pointer.
C++ lets objects destroy themselves with the source code statement: delete this.

C#

The keyword this in C# works the same way as in Java, for reference types. However, within C# value types, this has quite different semantics, being similar to an ordinary mutable variable reference, and can even occur on the left side of an assignment.
One use of this in C# is to allow reference to an outer field variable within a method that contains a local variable that has the same name. In such a situation, for example, the statement var n = localAndFieldname; within the method will assign the type and value of the local variable localAndFieldname to n, whereas the statement var n = this.localAndFieldname; will assign the type and value of the outer field variable to n.

D

In D this in a class, struct, or union method refers to an immutable reference of the instance of the enclosing aggregate. Classes are reference types, and structs and unions are value types. In the first version of D, the keyword this is used as a pointer to the instance of the object the method is bound to, while in D2 it has the character of an implicit ref function argument.

Dylan

In the programming language Dylan, which is an object-oriented language that supports multimethods and doesn't have a concept of this, sending a message to an object is still kept in the syntax. The two forms below work in the same way; the differences are just syntactic sugar.
object.method
and
method

Eiffel

Within a class text, the current type is the type obtained from the current class. Within features of a class, one may use the keyword Current to reference the current class and its features. The use of the keyword Current is optional as the keyword Current is implied by simply referring to the name of the current class feature openly. For example: One might have a feature `foo' in a class MY_CLASS and refer to it by:

class
MY_CLASS

feature -- Access

foo: INTEGER

my_function: INTEGER
do
Result := foo
end
end

Line #10 has the implied reference to Current by the call to simple `foo'.
Line #10 has the explicit reference to Current by the call to `Current.foo'.

class
MY_CLASS

feature -- Access

foo: INTEGER

my_function: INTEGER
do
Result := Current.foo
end
end

Either approach is acceptable to the compiler, but the implied version is preferred as it is less verbose.
As with other languages, there are times when the use of the keyword Current is mandated, such as:

class
MY_CLASS

feature -- Access

my_command
-- Create MY_OTHER_CLASS with `Current'
local
x: MY_OTHER_CLASS
do
create x.make_with_something
end
end

In the case of the code above, the call on line #11 to make_with_something is passing the current class by explicitly passing the keyword Current.