5 ms·
Brian Goetz's take on generics: http://cr.openjdk.java.net/~briangoetz/valhalla/erasure.html http://cr.openjdk.java.net/~briangoetz/valhalla/erasure.html
by hashmash 5y ago
Brian Goetz's take on generics: http://cr.openjdk.java.net/~briangoetz/valhalla/erasure.html http://cr.openjdk.java.net/~briangoetz/valhalla/erasure.html
- Joker_vD 5y ago> The language actually provides quite a strong safety guarantee for generics, as long as we follow the rules: If a program compiles with no unchecked or raw warnings, the synthetic casts inserted by the compiler will never fail. Huh. That was written in 2020, four years after it was shown how to write a very small program that "compiles with no unchecked or raw warnings", and yet "the synthetic casts inserted by the compiler" will fail at run time [0]: class Unsound { static class Constrain<A, B extends A> {} static class Bind<A> { <B extends A> A upcast(Constrain<A, B> constrain, B b) { return b; } } static <T, U> U coerce(T t) { Constrain<U, ? super T> constrain = null; Bind<U> bind = new Bind<U>(); return bind.upcast(constrain, t); } public static void main(String[] args) { String zero = Unsound.<Integer, String>coerce(0); } } [0] N. Amin, R. Tate "Java and Scala's type systems are unsound: the existential crisis of null pointers", https://dl.acm.org/doi/pdf/10.1145/2983990.2984004 https://dl.acm.org/doi/pdf/10.1145/2983990.2984004
- hashmash 5y agoThe sample program doesn't compile: Unsound.java:16: error: method upcast in class Bind<A> cannot be applied to given types; return bind.upcast(constrain, t);
- overgard 5y agoThat's definitely interesting, but I feel like C#'s approach of just creating a new set of containers for generics aged way better. It feels silly that I'm paying a tax in 2021 on a decision made in 2004 to be backwards compatible to code written in like 1996