very dumb question, why does idea complain about u...
# getting-started
t
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
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
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