This is just a thought I had. I don't expect, or even really hope, that it should happen. Just sharing some thoughts, I suppose.
A somewhat novel idea occurred to me. At runtime, the only thing that actually stops the CLR from invoking an instance method on a null reference is a runtime check for a null value. If such a check were removed, we could invoke a non-virtual instance method on a null reference, resulting in a this pointer with a null value. The natural question would be, "What person in their right mind would want to call instance methods on nothing in particular an OOP language?"
There are times, however, when this could be handy. Consider a common problem with strings. We might assume a string variable has a value, even if the value is empty. This mistake can result is a null reference exception when we check the length of a string, change its case, or call the instance Equals method. With "nullable" string methods, however, we could write a Length property that looked like this:
The behavior we achieve here is that even if we read the Length property on a null reference, we get the intuitive return value of zero. Of course, this "feature" would not be without drawbacks. We take the non-null value of the this pointer for granted, and especially with unqualified class members, it would be all too easy to add null-reference exceptions to your program instead of reducing them.
So, am I the only who cooks up wacky programming features, or does anyone else have anything to add?
A somewhat novel idea occurred to me. At runtime, the only thing that actually stops the CLR from invoking an instance method on a null reference is a runtime check for a null value. If such a check were removed, we could invoke a non-virtual instance method on a null reference, resulting in a this pointer with a null value. The natural question would be, "What person in their right mind would want to call instance methods on nothing in particular an OOP language?"
There are times, however, when this could be handy. Consider a common problem with strings. We might assume a string variable has a value, even if the value is empty. This mistake can result is a null reference exception when we check the length of a string, change its case, or call the instance Equals method. With "nullable" string methods, however, we could write a Length property that looked like this:
C#:
// nullable keyword to declare intent to allow null references
public nullable int Length{
get{
if(this == null)
return 0;
else
return charbuffer.Length;
}
}
So, am I the only who cooks up wacky programming features, or does anyone else have anything to add?