https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thanksforallthefish

06/09/2022, 5:44 AM
very dumb question, why does idea complain about unsafe cast in
Copy code
fun cast(value: Any?) = (value as? Map<String, String?>)
? maybe followup question, am I misusing
as?
? context, in this scenario I know that
value
might not be a map, so using
as?
on purpose since it returns
null
, but this “null-trick” makes the cast safe in my eyes, since `null`s need to be handled explicitly
j

Joffrey

06/09/2022, 6:28 AM
The unsafe part of the cast is the generics on the map. The cast will succeed on any Map (not return null) due to type erasure, even on maps with other types as keys or values, and the compiler is warning you that this will break the type safety
☝️ 2
t

thanksforallthefish

06/09/2022, 6:38 AM
ah yes, that ofc. and looking at warning more carefully it does indeed complain about the
String
part, somehow I thought it was complaining about the
Map
6 Views