Here’s a question I’m asked occasionally:
void M<T>(T t) where T : Animal
// This gives a compile-time error:
if (t is Dog)
// But this does not:
if (t is Dog)
(t as Dog).Bark();
What’s going on here? Why is the cast operator rejected? The reason illustrates yet again that the cast operator is super weird.
I briefly discussed copy-paste errors in code earlier; though this is a rich area of defects that I will probably at some point go into more detail on, that’s not for today.
Though this is a trivial little issue, I think it is worthwhile to illustrate how to think about these sorts of defects.
What is the defect?
Last week Jon Skeet “tweeted” humorously that in his work on the ECMA committee that is standardizing C#, they had found a mistake in the specification that was probably my fault; some commenters suggested that perhaps hell had also frozen over.
The latest episode of Coverity’s series Ask the Bug Guys is now posted on the Development Testing Blog. In this episode, a reader get me to play “guess the type”, and my colleague Tim talks a bit about the Java equivalent of the C#
using statement for releasing resources. Check it out!
As always, if you have a question about an odd bit of C#, C, C++ or Java that led to a bug, send it to
TheBugGuys@coverity.com; we’d love to see it. We can’t guarantee an answer to all your problems, but we will pick a selection of the best questions and post about them on the dev testing blog.
Suppose we have my usual hierarchy of types,
Giraffe, etc, with the obvious type relationships. An
IEqualityComparer<T> is contravariant in its type parameter; if we have a device which can compare two
Animals for equality then it can compare two
Giraffes for equality as well. So why does this code fail to compile?
IEqualityComparer<Animal> animalComparer = whatever;
IEnumerable<Giraffe> giraffes = whatever;
IEnumerable<Giraffe> distinct = giraffes.Distinct(animalComparer);
This illustrates a subtle and slightly unfortunate design choice in the method type inference algorithm, which of course was designed long before covariance and contravariance were added to the language.
The C# specification defines
foreach (V v in x)
as having the same semantics as:
E e = ((C)(x)).GetEnumerator();
V v; // Inside the while in C# 5.
v = (V)e.Current;
// necessary code to dispose e
(Actually this is not exactly what the spec says; I’ve made one small edit because I don’t want to get into the difference between the element type and the loop variable type in this episode.)
There are a lot of subtleties here that we’ve discussed before; what I want to talk about today is the explicit conversion from
V. On the face of it this seems very problematic; that’s an explicit conversion. The collection could be a list of
V could be
int; normally C# would not allow a conversion from
int without a cast operator appearing in the source code. (Or the
long being a constant that fits into an
int.) What justifies this odd design choice?
I’ve talked a lot about floating point math over the years in this blog, but a quick refresher is in order for this episode.
double represents a number of the form
+/- (1 + F / 252 ) x 2E-1023, where
F is a 52 bit unsigned integer and
E is an 11 bit unsigned integer; that makes 63 bits and the remaining bit is the sign, zero for positive, one for negative. You’ll note that there is no way to represent zero in this format, so by convention if
E are both zero, the value is zero. (And similarly there are other reserved bit patterns for infinities, NaN and denormalized floats which we will not get into today.)
decimal represents a number in the form
+/- V / 10X where
V is a 96 bit unsigned integer and
X is an integer between 0 and 28.
Both are of course “floating point” because the number of bits of precision in each case is fixed, but the position of the decimal point can effectively vary as the exponent changes.
Today a follow-up to my 2010 article about the meaning of the
is operator. Presented as a dialog, as is my wont!
I’ve noticed that the
is operator is inconsistent in C#. Check this out:
string s = null; // Clearly null is a legal value of type string
bool b = s is string; // But b is false!
What’s up with that?
Let’s suppose you and I are neighbours.
Um… ok, I’m not sure where this is going, but sure.
Returning now to the subject we started discussing last time on FAIC: sometimes the compiler can know via static analysis[1. That is, analysis done knowing only the compile-time types of expressions, rather than knowing their possibly more specific run-time types] that an
is operator expression is guaranteed to produce a particular result.
As I said last time, that was a pretty easy puzzle. The solution is: either
FooBar or the type of local variable
x can be a type parameter:
It is possible for a program with some local variable
bool b = x is FooBar;
to assign true to
b at runtime, even though there is no conversion, implicit or explicit, from
FooBar allowed by the compiler! That is to say that
FooBar foobar = (FooBar)x;
would not be allowed by the compiler in that same program.
Can you create a program to demonstrate this fact?
This is not a particularly hard puzzle but it does illustrate some of the subtleties of the
is operator that we’ll discuss in the next episode.