Why do some claim that Java's implementation of generics is bad?

后端 未结 13 1968
挽巷
挽巷 2020-11-29 15:53

I\'ve occasionally heard that with generics, Java didn\'t get it right. (nearest reference, here)

Pardon my inexperience, but what would have made them better?

13条回答
  •  悲哀的现实
    2020-11-29 16:24

    Ignoring the whole type erasure mess, generics as specified just don't work.

    This compiles:

    List x = Collections.emptyList();
    

    But this is a syntax error:

    foo(Collections.emptyList());
    

    Where foo is defined as:

    void foo(List x) { /* method body not important */ }
    

    So whether an expression type checks depends on whether it is being assigned to a local variable or an actual parameter of a method call. How crazy is that?

提交回复
热议问题