How does exactly as? behave in kotlin?
Solution 1:
When you cast a class that has generic types, the cast will always succeed if the generic type is the only thing that is wrong. And type erasure, which you seem to be familiar with, is the reason. At runtime, a List<String>
and a List<Foo>
are exactly the same thing because of type erasure so casting between them will always succeed.
So a safe cast does not protect you from getting the generic type wrong, only the class type.