Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the 'this' variable of value types

Apparently you can change the this value from anywhere in your struct (but not in classes):

struct Point
{
    public Point(int x, int y)
    {
        this = new Point();
        X = x; Y = y;
    }
    int X; int Y;
}

I've neither seen this before nor ever needed it. Why would one ever want to do that? Eric Lippert reminds us that a feature must be justified to be implemented. What great use case could justify this? Are there any scenarios where this is invaluable? I couldn't find any documentation on it1.

Also, for calling constructors there is already a better known alternative syntax, so this feature is sometimes redundant:

public Point(int x, int y)
    : this()
{
    X = x; Y = y;
}

I found this feature in an example in Jeffrey Richter's CLR via C# 4th edition.
1) Apparently it is in the C# specification.

like image 737
Daniel A.A. Pelsmaeker Avatar asked Feb 08 '13 02:02

Daniel A.A. Pelsmaeker


People also ask

How do you change the type of a variable?

You can change the data type for a variable at any time by using the variable Type setting in the Variables tab. Existing values are converted to the new type. If no conversion is possible, the system-missing value is assigned.

Which are used to change the values of variables?

Changing the Values of Variables. You can change the value of any variable or the contents of any memory location displayed in a Variable Window, Expression List Window, or Stack Frame Pane by selecting the value and typing the new value. In addition to typing a value, you can also type an expression.

Can you change the value in a variable?

You can "assign a new value" to your variable, but you can't change the value itself, unlike an array or object, which you can modify without affecting the variable binding.

What is a value type variable?

Reference Type variables are stored in the heap while Value Type variables are stored in the stack. Value Type: A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value.

How to change any number to a string type variable?

To change any number to a string type variable, you have to use the str () function. The function takes the number as an argument to change to string type. See the example below that shows the number and the method to convert to string. The output in the above example shows the string type result.

How do I change the value of a variable in Python?

The first and easiest way of changing the value of a variable is simply to reassign it: Here, we declare the variable numberOfCats and initialize it with the value 3.

How do you modify the contents of a variable?

Modify the contents of a variable by reassigning it or with operators. Use constants to prevent essential pieces of data from being over-written. In the next chapter, we will have a look at an important attribute of variables: their type.

How do I change the type of a variable in SPSS?

In such cases, changing these is often the way to go. First note that there's really only two variable types in SPSS: string and numeric. For more on this, see SPSS Numeric Variables Basics. Starting from SPSS version 16, one can use ALTER TYPE in order to change a variable's type.


2 Answers

Good question!

Value types are, by definition, copied by value. If this was not actually an alias to a storage location then the constructor would be initializing a copy rather than initializing the variable you intend to initialize. Which would make the constructor rather less useful! And similarly for methods; yes, mutable structs are evil but if you are going to make a mutable struct then again, this has to be the variable that is being mutated, not a copy of its value.

The behaviour you are describing is a logical consequence of that design decision: since this aliases a variable, you can assign to it, same as you can assign to any other variable.

It is somewhat odd to assign directly to this like that, rather than assigning to its fields. It is even more odd to assign directly to this and then overwrite 100% of that assignment!

An alternative design which would avoid making this an alias to the receiver's storage would be to allocate this off the short-term storage pool, initialize it in the ctor, and then return it by value. The down side of that approach is that it makes copy elision optimizations pretty much impossible, and it makes ctors and methods weirdly inconsistent.

like image 169
Eric Lippert Avatar answered Oct 24 '22 00:10

Eric Lippert


Also, I couldn't find any documentation on it.

Did you try looking in the C# spec? Because I can find documentation on it (7.6.7):

  • When this is used in a primary-expression within an instance constructor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs, and the variable represents the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the same as an out parameter of the struct type—in particular, this means that the variable must be definitely assigned in every execution path of the instance constructor.

  • When this is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs.

    • If the method or accessor is not an iterator (§10.14), the this variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as a ref parameter of the struct type.
    • If the method or accessor is an iterator, the this variable represents a copy of the struct for which the method or accessor was invoked, and behaves exactly the same as a value parameter of the struct type.

As to a use case for it, I can't immediately think of many - about the only thing I've got is if the values you want to assign in the constructor are expensive to compute, and you've got a cached value you want to copy into this, it might be convenient.

like image 36
Damien_The_Unbeliever Avatar answered Oct 24 '22 02:10

Damien_The_Unbeliever