Re: why not compile error on "incompatible interface cast"?
gg9h0st wrote:
Doesn't "cast operator" know it's actuall object?
em... cast operator test it's operand as a refernce variable, not far
to the object in compile stage.
and extending a class should be clear to test whle implementing a
interface is not.
is that right?
I don't totally understand what you're trying to say, but I'll take a
gander.
In principle, there is sufficient information at compile time to be
determine that the cast will fail. Such analysis, though, is impractical
or even impossible in all cases. I am not certain, but I believe that if
ClassB were final, the cast would be a compiler error.
A compiler sees the statement as "cast reference of type T1 to a
reference of type T2"; it would need additional information to be stored
to be able to prove that the object cannot, in fact, be type T2. Tools
which perform such analysis do exist, and fall under the heading of
"static analysis," which retains more information about statements in
order to detect which ones could be or are problematic. FindBugs is a
classic example of such a tool for Java.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth