Should "a is b" compile if a and b have unrelated classes?

Michael Coulombe kirsybuu at gmail.com
Tue Apr 10 21:52:22 UTC 2018


I had a bug in my code that was messing me up for a while, and it 
boiled down to an identity check between two Object references 
with unrelated static types, like below:

class A {}
class B {}
void main() {
     A a = new A;
     B b = new B;
     if (a is b) {} // compiles
}

I was surprised that the type system failed me here. It's true 
that A and B could be upcast to Object and then comparisons would 
make sense type-wise, but the comparison should never pass (and 
the compiler should know it won't since they are in separate 
inheritance subtrees) unless the programmer is intentionally 
breaking the type system.

Is there reasoning for this? If not, should it be a warning or 
error, as it is for example when comparing two pointers to 
structs of different types?


More information about the Digitalmars-d-learn mailing list