4 ms·
This, imo, is actually a bigger deal than generics. Since type erasure does all kinds of "magic", it doesn't surprise me that there will end up some kind of (T)
by dvt 10y ago
This, imo, is actually a bigger deal than generics. Since type erasure does all kinds of "magic", it doesn't surprise me that there will end up some kind of (T)-type casts somewhere in the OP's bytecode.
But the fact that the following is unsound (and has been for a while) will always remain a mystery to me:
public class Main {
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public static void main (String[] args) {
Main x = new Main();
Dog[] doges = new Dog[10];
x.t(doges);
}
public void t(Animal[] bar) {
bar[0] = new Cat();
bar[0] = new Animal(); // also breaks
}
}
The array type seems completely tractable and may be able to be caught by the compiler.
- trendkill 10y agoA joke from my childhood: Man goes to doctor, and says: "Gee, doc, it really hurts when I do this." Doctor replies: "Then don't do that."
- paulddraper 10y agoTwo men stand on a bridge. The first leans on the railing, which gives way and sends him over the edge. Horrified, the second looks around and spots a civil engineer. Engineer shrugs and says, "Don't do that."
- JoshTriplett 10y agoThis would be fine if t's array bar only allowed mutation of the objects within the array, but didn't allow mutation of the array itself. Treating a Dog as an Animal works fine; however, you can only treat a Dog[] as a const Animal[], not a mutable Animal[].
- mvindahl 10y agoOn the positive side, these kind of errors tend to blow up immediately when the code is executed. Assuming that you have a semi-decent test suite, it won't make it to production. The language type system won't save your butt but your unit tests will. There is a deeper discussion here; if you are writing good automatic tests anyway, which I hope you are, then of how much value is a rigorious type system? :)
- deleted 10y ago[deleted]
- greydius 10y agoI agree that a comprehensive test suite is valuable, but I don't think it's safe to assume that errors like this will always be caught, no matter how good your tests are. It's not reasonable to write a test for every possible situation that could ever occur. In fact, it's a giant waste of time. > of how much value is a rigorous type system? It saves a lot of time on those tests you won't have to write and maintain just to ensure that you're getting your types correct. Strong static types are also good documentation. Some type systems (e.g. Haskell) can even explicitly indicate whether a procedure is pure or has effects. Static types also enable dispatch that can be resolved at compile-time, resulting in more efficient execution. And the list goes on. Of course, advocates of languages without static types have heard and dismissed all these arguments before. I guess it's really just a matter of taste.
- mvindahl 10y agoI've heard both sides of the static types vs dynamic types argument a lot of times. I don't think there will ever be any kind of universal agreement but I find the discussion to be interesting. For the record, I personally think a language like Typescript hit a sweet spot. Not a lot of mandatory ceremony but you have the choice to add types where it matters, e.g. at interface level. Also the duck typing saves you from inventing class names for all kind of almost-primitive types.
- dllthomas 10y ago> if you are writing good automatic tests anyway, which I hope you are "Don't write tests. Generate them."
- krona 10y ago> The array type seems completely tractable and may be able to be caught by the compiler. This isn't an error, it was a design decision: https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Covariant_arrays_in_Java_and_C.23 https://en.wikipedia.org/wiki/Covariance_and_contravariance_... You could call it bad design, but fixing it would (imo) just make Java even worse than it already is.
- dvt 10y agoMy point had nothing to do with the merits of covariant types, just tractability. It seems that the array's type is tractable so the compiler should (at least) give a warning.