When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

The problem is that GenericTraversableTemplate is used twice: once for mutable collections (where its type parameter should be invariant), and once for immutable collections (where covariance is invariably king). GenericTraversableTemplate’s typechecks assuming either covariance or invariance for the A type parameter. However, when we inherit it in a mutable trait, we have to pick invariance. … Read more

Why does javac complain about generics unrelated to the class’ type arguments? [duplicate]

From JLS 4.8 Raw Types The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. and The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static … Read more

Type safety: Unchecked cast

The problem is that a cast is a runtime check – but due to type erasure, at runtime there’s actually no difference between a HashMap<String,String> and HashMap<Foo,Bar> for any other Foo and Bar. Use @SuppressWarnings(“unchecked”) and hold your nose. Oh, and campaign for reified generics in Java 🙂