2 ms·
mammal = cow; // [1] pig = mammal; // [2] Checks OK statically, but now pig holds a Cow. print(pig.oink()); // [3] NoSuchMethodException if we get
by ot 15y ago
mammal = cow; // [1]
pig = mammal; // [2] Checks OK statically, but now pig holds a Cow.
print(pig.oink()); // [3] NoSuchMethodException if we get this far.
As far as I understand this means that no precondition can be encoded in the variable type. A function with a Pig argument can still cause a NoSuchMethodException exception because the argument can actually hold a Cow instance.
- jbri 15y agoYou still have a precondition encoded in the parameter type. It's just a precondition for the programmer to see and understand, rather than one for the machine to rigorously enforce.