I'm trying to check Serializable type before casting it to HashMap<String, String>. But it gives following warning;
Cannot check for instance of erased type: kotlin.collections.HashMap /* = java.util.HashMap */
Is there a way to check if Serializable is type of HashMap<String, String> then safe cast it?
params = if (it.getSerializable(ARG_PARAMS) is HashMap<String, String>) {
it.getSerializable(ARG_PARAMS) as HashMap<String, String>
} else null
Actually, you cannot check that your object has type HashMap<String, String> because type parameters are erased in runtime. I would like to suggest you just use a safe cast:
params = arguments?.getSerializable(ARG_PARAMS) as? HashMap<String, String>
Important:
It might be not clear but my code doesn't check that argument really has type
HashMap<String, String>. You can passHashMap<String, Int>and in some case, you'll get an error. In my snippet you just say to the compiler: "I know that there will beHashMap<String, String>. Just give it to me".paramswill be null only when there is no argument or its type differs fromHashMap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With