This (computer programming)

Last updated

this, self, and Me 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 (such as which object has its method called). 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.

Contents

Object-oriented programming

In many object-oriented programming languages, this (also called self or Me) 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. [1] :4.3.2.3 C++ and languages which derive in style from it (such as Java, C#, D, and PHP) 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. [2]

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 (for example GCC), pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit this pointer parameter. [3]

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:

usefeature":5.16";sub{my$x=shift;$x==0?1:$x*__SUB__->($x-1);}

By contrast, in C++ (using an explicit this for clarity, though not necessary) 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.

unsignedintfactorial(unsignedintn){if(n==0)return1;elsereturnn*this->factorial(n-1);}

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. [4] [5]

The fragile base class problem has been blamed on open recursion, with the suggestion that invoking methods on this default to closed recursion (static dispatch) rather than open recursion (dynamic dispatch), only using open recursion when it is specifically requested; external calls (not using this) would be dynamically dispatched as usual. [6] [7] 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: [8]

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. [9]

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. [10]

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. [11]

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(param1, param2)

and

method (object, param1, param2)

Eiffel

Within a class text, the current type is the type obtained from the current class. Within features (routines, commands and queries) 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:

classMY_CLASSfeature-- Accessfoo:INTEGERmy_function:INTEGERdoResult:=fooendend

[12]

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

Line #10 (below) has the explicit reference to Current by the call to `Current.foo'.

classMY_CLASSfeature-- Accessfoo:INTEGERmy_function:INTEGERdoResult:=Current.fooendend

Either approach is acceptable to the compiler, but the implied version (e.g. x := foo) 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:

classMY_CLASSfeature-- Accessmy_command-- Create MY_OTHER_CLASS with `Current'localx:MY_OTHER_CLASSdocreatex.make_with_something(Current)endend

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.

Java

The keyword this is a Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods.

Since all instance methods are virtual in Java, this can never be null. [13]

JavaScript

In JavaScript, which is a programming or scripting language used extensively in web browsers, this is an important keyword, although what it evaluates to depends on where it is used.

To work around the different meaning of this in nested functions such as DOM event handlers, it is a common idiom in JavaScript to save the this reference of the calling object in a variable (commonly called that or self), and then use the variable to refer to the calling object in nested functions.

For example:

// In this example $ is a reference to the jQuery library $(".element").hover(function(){// Here, both this and that point to the element under the mouse cursor.varthat=this;$(this).find('.elements').each(function(){// Here, this points to the DOM element being iterated.// However, that still points to the element under the mouse cursor.$(this).addClass("highlight");});});

Notably, JavaScript makes use of both this and the related keyword self [17] (in contrast to most other languages which tend to employ one or the other), with self being restricted specifically to web workers. [18]

Finally, as a reliable way of specifically referencing the global (window or equivalent) object, JavaScript features the globalThis keyword. [19]

Lua

In Lua, self is created as syntactic sugar when functions are defined using the : operator. [20] When invoking a method using :, the object being indexed will be implicitly given as the first argument to the function being invoked.

For example, the following two functions are equivalent:

localobj={}functionobj.foo(arg1,arg2)print(arg1,arg2)-- cannot use "self" hereendfunctionobj:bar(arg)print(self,arg)-- "self" is an implicit first argument before argend-- All functions can be invoked both ways, with "." or with ":"obj:foo("Foo")-- equivalent to obj.foo(obj, "Foo")obj.bar(obj,"Bar")-- equivalent to obj:bar("Bar")

Lua itself is not object-oriented, but when combined with another feature called metatables, the use of self lets programmers define functions in a manner resembling object-oriented programming.

PowerShell

In PowerShell, the special automatic variable $_ contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline. [21]

"one","two","three"|%{write $_}

Also starting with PowerShell 5.0, which adds a formal syntax to define classes and other user-defined types, [22] $this variable describes the current instance of the object.

Python

In Python, there is no keyword for this. When a member function is called on an object, it invokes the member function with the same name on the object's class object, with the object automatically bound to the first argument of the function. Thus, the obligatory first parameter of instance methods serves as this; this parameter is conventionally named self, but can be named anything.

In class methods (created with the classmethod decorator), the first argument refers to the class object itself, and is conventionally called cls; these are primarily used for inheritable constructors, [23] where the use of the class as a parameter allows subclassing the constructor. In static methods (created with the staticmethod decorator), no special first argument exists.

Rust

In Rust, types are declared separately from the functions associated with them. Functions designed to be analogous to instance methods in more traditionally object-oriented languages must explicitly take self as their first parameter. These functions can then be called using instance.method() syntax sugar. For example:

structFoo{bar: i32,}implFoo{fnnew()-> Foo{Foo{bar: 0,}}fnrefer(&self){println!("{}",self.bar);}fnmutate(&mutself,baz: i32){self.bar=baz;}fnconsume(self){self.refer();}}

This defines a type, Foo, which has four associated functions. The first, Foo::new(), is not an instance function and must be specified with the type prefix. The remaining three all take a self parameter in a variety of ways and can be called on a Foo instance using the dot-notation syntax sugar, which is equivalent to calling the type-qualified function name with an explicit self first parameter.

letmutfoo=Foo::new();// must called as a type-specified functionfoo.refer();// prints "0". Foo::refer() has read-only access to the foo instancefoo.mutate(5);// mutates foo in place, permitted by the &mut specification, need foo to be declared mutfoo.consume();// prints "5" and destroys foo, as Foo::consume() takes full ownership of self//  equivalent to foo.refer()Foo::refer(foo);// compilation error: foo is out of scope

Self

The Self language is named after this use of "self".

Xbase++

Self is strictly used within methods of a class. Another way to refer to Self is to use ::.

See also

Related Research Articles

Prototype-based programming is a style of object-oriented programming in which behavior reuse is performed via a process of reusing existing objects that serve as prototypes. This model can also be known as prototypal, prototype-oriented,classless, or instance-based programming.

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are the values of the arguments with which the subroutine is going to be called/invoked. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.

In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time.

In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions as well. In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.

In computer programming, a sigil is a symbol affixed to a variable name, showing the variable's datatype or scope, usually a prefix, as in $foo, where $ is the sigil.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time for each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

<span class="mw-page-title-main">Python syntax and semantics</span> Set of rules defining correctly structured programs

The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. It supports multiple programming paradigms, including structured, object-oriented programming, and functional programming, and boasts a dynamic type system and automatic memory management.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

In computer programming, a constant is a value that is not altered by the program during normal execution. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution. To simplify, constants' values remains, while the values of variables varies, hence both their names.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community. Swift compiles to machine code and uses an LLVM-based compiler. Swift was first released in June 2014 and the Swift toolchain has shipped in Xcode since version 6, released in 2014.

References

  1. Dahl, Ole-Johan; Myhrhaug, Bjørn; Nygaard, Kristen (1970). "Common Base Language, Norwegian Computing Center".
  2. Powell, Thomas A, and Schneider, Fritz, 2012. JavaScript: The Complete Reference, Third Edition. McGraw-Hill. Chapter 11, Event Handling, p 428. ISBN   978-0-07-174120-0
  3. Using the GNU Compiler Collection (GCC) Bound member functions
  4. "Closed and Open Recursion", Ralf Hinze, July 2007
  5. Open Recursion, Lambda the Ultimate
  6. "Selective Open Recursion: A Solution to the Fragile Base Class Problem", Jonathan Aldrich
  7. "Selective Open Recursion: A Solution to the Fragile Base Class Problem", Lambda the Ultimate
  8. Aldrich, Jonathan, and Kevin Donnelly. "Selective open recursion: Modular reasoning about components and inheritance." SAVCBS 2004 Specification and Verification of Component-Based Systems (2004): 26. citing for the JDK-adopted solution C. Ruby and G. T. Leavens. "Safely Creating Correct Subclasses without Seeing Superclass Code". In Object-Oriented Programming Systems, Languages, and Applications, October 2000. doi : 10.1145/353171.353186 also available as technical report TR #00-05d
  9. ISO/IEC 14882:2003(E): Programming Languages - C++. ISO/IEC. 2003.
  10. Stroustrup: C++ Style and Technique FAQ
  11. De Smet, Bart, 2011. C# 4.0 Unleashed. Sams Publishing, Indianapolis, USA. Chapter 4, Language Essentials, p 210. ISBN   978-0-672-33079-7
  12. NOTE: The line numbers are for reference purposes only. Eiffel does not have line numbers in the class text. However, there is a line number option in the Eiffel Studio IDE, which can be optionally turned on for reference purposes (e.g. pair programming, etc).
  13. Barnes, D. and Kölling, M. Objects First with Java. "...the reason for using this construct [this] is that we have a situation that is known as name overloading - the same name being used for two different entities... It is important to understand that the fields and the parameters are separate variables that exist independently of each other, even though they share similar names. A parameter and a field sharing a name is not a problem in Java."[ citation needed ]
  14. Crockford, Douglas, 2008. JavaScript: The Good Parts. O'Reilly Media Inc. and Yahoo! Inc. Chapter 4, Functions, p 28. ISBN   978-0-596-51774-8
  15. Powell, Thomas A, and Schneider, Fritz, 2012. JavaScript: The Complete Reference, Third Edition. McGraw-Hill. Chapter 5, Functions, pp 1701. ISBN   978-0-07-174120-0
  16. Goodman, Danny, with Morrison, Michael, 2004. JavaScript Bible, 5th Edition. Wiley Publishing, Inc., Indianapolis, USA. Chapter 33, Functions and Custom Objects, p 987. ISBN   0-7645-5743-2
  17. Mozilla Developer Network: Window.self
  18. Mozilla Developer Network: Web Worker API
  19. Mozilla Developer Network: globalThis
  20. "Programming in Lua : 16".
  21. msdn. "PowerShell: About Automatic Variables". docs.microsoft.com. Retrieved 2018-03-22.
  22. msdn. "about_Classes". docs.microsoft.com. Retrieved 2018-12-17.
  23. Unifying types and classes in Python 2.2, Guido van Rossum, "Overriding the __new__ method"

Further reading