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?

Reply via email to