If T
is a parameterized type, then isInstanceOf[T]
must give an erasure warning, even if we have a ClassTag[T]
in scope.
Alternatively, we could ensure that ClassTag[T]
does not exist for parameterized types, but only for their wildcard versions. More alternatives might be desirable, since ClassTag[T]
is also used for array creation and that has different requirements.
This code (files below) demonstrates why.
The issue is that we get heap pollution and a ClassCastException
without casts or erasure warnings, and that must not happen, as described by Java's guarantees for erasure warnings, which I expect should apply to us too:
https://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html
Googling instanceof classtag unsound finds nothing, but somebody must have noticed? I expect I never did, but this issue is surprisingly easy to trigger. Apparently shapeless's Typeable takes care of avoiding the issue: