3 ms·
Here's another fun trick, though it's probably not a flaw in the type system: https://twitter.com/joshbloch/status/1018987317489426432 https://twitter.com/joshb
by hyperpape 8y ago
Here's another fun trick, though it's probably not a flaw in the type system: https://twitter.com/joshbloch/status/1018987317489426432 https://twitter.com/joshbloch/status/1018987317489426432.
- BonesJustice 8y agoYeah, 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.
- dlubarov 8y agoAnother 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.