InstanceOf

From APIDesign

Jump to: navigation, search

Properly used Covariance is said to be source compatible, but there is one language contruct which can lead to broken code - instanceof. The JavaC acts cleverly and checks not just lower bound of the first parameter, but upper bound. For example following code is not going to compile:

String s = "";
assert s instanceof Integer;

The JavaC complains about inconvertible types. Simple change to the program fixes the problem:

Object s = "";
assert s instanceof Integer;

Knowing this JavaC behavior, I am sure you can write code that will compile with the API used in Covariance example version 1.0 and will break the compilation against version 2.0. Am I right?

Personal tools
buy