Hacker News new | ask | show | jobs
by tsimionescu 1992 days ago
This soundness hole (or a very similar one) was also accepted in Java itself (with subtypes rather than sum types).

The main reason behind accepting it is that it is very useful and safe in certain situations that the type system is too weak to define strictly: if a function takes an (string|number)[] and only reads from it, it is perfectly safe to pass in a string[].

2 comments

Only arrays (the only generic type that predates Java 5) have this problem in Java, and for precisely this reason, modern Java practice avoids arrays in favor of other collection types. When real generics were introduced in Java 5, they used a design with proper variance that doesn't have this problem.
Well, that depends on context and language. I know at least on language where the binary representation of String and String|Int is different and where the difference matters.