Type casts



I’m searching for this topic for quite a while now. I’m trying to get rid of some warnings and would like to know how to do that.

val documentMap = parseJson(json)
if (documentMap is MutableMap<*, *>) {
  return Document(documentMap as MutableMap<String, Any?>)
} else throw Exception(“unknown structure”)

The third line produces an unchecked cast warning, which is right because documentMap is smart-casted to MutableMap<*, >. As described in http://confluence.jetbrains.com/display/Kotlin/Generics, the “is”-check can only be done with MutableMap<, *> and not directly against MutableMap<String, Any?>. How can I get rid of the warning that the unsafe “as”-cast produces?

Best regards


We do not support "suppress warnings" yet, but we will. Also, for the case when your documentMap is, say, Map<String, Any?>, we'll make the compiler smarter, so it'll figure out that your casts are OK.


Thanks for the quick reply. Can you tell, if the smater compiler will be there in far or near future?


I'm going to work on it in the next few days. The "suppress warnings" part will come a little later


Thanks! I'm going to watch out for new releases.

I personally don’t care much about the feature to supress warnings. Always better to fix a warning than to supress it :wink:

From a developer point of view, it would be cool too if a smart-cast could cast directly to Map<String, Any?> instead of the generic variant Map<*, *>. But as I understand the documentation, that would have a bigger impact on kotlin internals and I guess it’s do-able in appropiate time.

Thanks for all the great work, that have be done so far.


This functionality is now available in the fresh nightly builds.


Great! It seems to work now. Even the suppress warning seem to work. Thanks a lot.