In the class definitions of an object oriented language, this
refers to the object instance the code is about to work upon. Naturally, it should have the same type as the class to be defined. But there are variations of a type that could also be applied to this
. self
is sometimes an alternative name to this
, but sometimes could also refer to the class itself, instead of the object.
To demonstrate, a simple example in C++ would be:
class T {
public:
int f() const {
// "this" is in (const T*), instead of just (T*)
}
}
The answer may seem obvious at first glance: For every variation that could be applied to the parameters, it could also be applied to this
. If parameters could be a constant, this
could be constant. If parameters could be a rvalue reference, this
could be defined as a rvalue reference. (There could be another question for that if anyone is interested.) Object oriented programming works like syntactic sugar over the traditional procedural programming in many cases, after all.
But there are also situations that this logic couldn't be applied easily. Notably, the curiously recurring template pattern (CRTP) refers to something like this:
class Derived : public Base<Derived>
It is implied that code in Base
may need to access the type Derived
somewhere. Derived
is something like the real type of this
. CRTP has the obvious disadvantages such as being less intuitive, and making casts between base classes and subclasses difficult. It might be better if we turn it into another variation of this
.
But template and generics are too general, that could be used as catch-all solutions to implement many different things, even if it is not optimal. Having CRTP may even make people less likely to report their use case, which may not be covered in a specific variation-of-this
solution.
So, what are the variations to the type of this
or self
that should be supported? I.e. what useful differences could be made to this
or self
, from just an object of the class to be defined, or just the class, using different declarations of a block of code?
CRTP could be a source of many different variations. But there might be also cases not covered by CRTP.
Note
- This is not about whether
this
should be a pointer or a reference, or implicit, as they are almost equivalent. I'm more interested in things that could not be done in other types ofthis
. I.e. It's objectively a valid answer if something could not be done, or could not be done safely in languages without one kind of variation. Say if we have a language like C++ that doesn't support the const qualifier in the example, calling a method in a constant object would be either impossible, or unsafe as it would use the non-constthis
in the method. So const is a valid answer, although trivial and already widely known. - This is not a survey about how things are done in every language. It's about what are the use cases to be covered, or how problems are solved if there are multiple ways.
- The very traditional things that are applied to any parameters, such as const and rvalue reference, should be better in a separate question, if there is. But they could be referenced if there are some overlaps.
this
?". It explicitly rules out substantiating evidence, and requiring strong uniqueness between answers doesn't seem to fit the Q+A model here. I'm not sure we're equipped to deal with this question as it's currently set out, and I'm not sure what answer it expects except the reference one - but as you say, that wouldn't fit the question either. $\endgroup$