Hacker News new | past | comments | ask | show | jobs | submit login

Here's another fun trick, though it's probably not a flaw in the type system: https://twitter.com/joshbloch/status/1018987317489426432.



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.


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: