Nullable and NonNull Type Cast


Why the compiler allows the cast between a nullable and non-null?

fun test(text: CharSequence?): String {
    return (text as String)

this code compiles, and once you send a non-null text it works, but as it allows nullables, the following happens when you send null:

kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String

P.S. I know cast is a dangerous operation, but once kotlin proposes the null-safety I don’t see why do not check to casts, so it is the motivation of my question.


Because a typecast is by itself already an unsafe operation. A typecast (especially in Kotlin with autocasts) is basically saying to the compiler that you know more than it does. The compiler says fine, but if it breaks it’s on you (you get to keep the pieces).

In other words text could just as well be a StringBuilder as null.


Thank you for your answer Paul.

I’m wondering if it is a good idea to the compiler check at least the null possibility. So, given that sample, the compiler will force me to do something like text as String? or text ?: "" as String.

I’m mean, it is still unsafe and will break when you send a StringBuilder, but it will be a typecast exception not related to null non-null types.