Re:The Java type system is broken
by hyperpape on Thursday July 19, 2018 @09:57AM
(#17566280)
Here's another fun trick, though it's probably not a flaw in the type system: https://twitter.com/joshbloch/status/1018987317489426432.
[ Parent
| on HN ]
Another flaw we can abuse is Java's covariant array types:
Object[] objects = new Integer[] {1, 2, 3};
objects[0] = "hello";
Also compiles but fails at runtime.
Re:The Java type system is broken
by BonesJustice on Thursday July 19, 2018 @01:54PM
(#17568787)
Yeah, compound types like that are a bit of a hack. During type erasure, the type variable gets reduced to its leftmost type (in this case, `String`), so the `main` signature ends up being a valid entry point. Best the compiler can do to enforce the right-hand type constraints is to insert cast instructions where values are used as insances of those types.
As the tweet shows, you still get (some) type safety from the runtime checks.