.comment-link {margin-left:.6em;}

Saturday, March 05, 2005

 

SO WHAT exactly is a variable type ?

 

I asked  Ian Griffiths  about "SO WHAT exactly is a variable type" ? i think it is the most important question in .NET . for me this is the most exciting question i have faced during the last 6 years about .NET .  what exactly is a variable type ?
let's take an example : String s  = "Something " ;  so the question is "is the LHS and RHS are both of type string?"
another example : Parent P = new Child ();  again the same question "is the type of the LHS is Parent or child ? "

this is Ian Answer (copy of his email) :

There’s one question you asked, which is actually ambiguous:

> So what exactly is the type of the LHS ?

The key to understanding this issue is to understand why that question is ambiguous. And to grasp, that, I ask this question in return:

"What do you mean by ‘the type’?"

 

There are at least 3 answers to this question that seem reasonable, and which contradict one another:

  1. it’s the type of whatever the variable refers to, so in your examples, the type of ‘s’ is String, and the type of ‘p’ is Child.
  2. It’s the type associated with the named location the variable represents, so ‘s’ would be String, and ‘p’ would be Parent
  3. its type is something else
  4. it does not have a type – in the .NET type system, you cannot define a type that represents what a variable is

To understand why 3 or 4 might even be options, let’s look at why neither 1 nor 2 can be strictly correct.

The interesting thing is that both 1 and 2 are useful ideas – in some situations, one or other of these is the way you will want to look at the code. However, it’s clear that 1 is an approximation because it’s really looking at the type of the thing being referred to rather than the variable itself.

But what’s wrong with 2? Well, let’s consider again your example:

String s = "Someone";

According to both 1 and 2, the type of ‘s’ is String. But it’s easy to prove that this is wrong. The String type is immutable. This is well documented – there’s nothing you can do to a string to change it. All of the methods on String that look like they’re going to change it (Trim, ToUpper etc.) leave the String unmodified, and instead create a new String that is the modified version of the original.

So, we know that String is immutable. If ‘s’ is of type String, then it must also be immutable. But it’s not – this works:

s = "Something";

We can change ‘s’. We can’t change the String it refers to, but we can change ‘s’ so that it refers to a whole new String.

In other words, there are thing we can do to this variable ‘s’ that we cannot do to a String. From this, we have to conclude that ‘s’ is not something of type String. The variable is associated with type String – it is a named location which is only able to hold references to String objects. But the variable itself is not a String.

So if you consider the following two statements:

it should now be clear that they don’t mean the same thing. If an object is of type String, we mean it’s a thing that is instance of the String class. That’s not what we mean when we say that a variable is of type String – it’s a shorthand for something else. It means that the variable is a thing that is able to hold a reference to a thing that is an instance of the String class.

So variables are associated with a type – it determines what they are able to refer to. But these variables themselves are distinct from the things they refer to. So do the variables themselves have a type?

There’s one case where variables clearly do have a type – and that’s when you’re using value types. Consider this:

int i;

In this case, the variable doesn’t refer to an int. It is an int. The value is the variable. This is true for all value types – when you declare a variable of a value type, that variable is an instance of that type. In these cases, both 1 and 2 above are correct.

So the question I originally posed only really pertains to reference types. And the essence of the question is whether it’s 3 or 4 above – is there something in the type system of .NET that represents the variable itself, whose behaviour is demonstrably different from the behaviour of the type(s) it is able to refer to.

 

Is that any clearer? I know I’ve still left the question unanswered. But that’s intentional – I’m just trying to make it clear that the answer is not obvious. Do you see the problem now?

 


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?