Hi, not sure if I am right herem but as it has something to do with compiling I was thinking maybe here?
It is about the smartCast. I appreciate that not all possibilities are covered, but I thought this scenario would work. I seem to be wrong? Or I did not add something which enables smartcast?
I have the following sample function:
Copy code
fun test(){
val testString: String? = null
testString ?: throw IllegalArgumentException("null")
print(testString.length)
}
I now want to extract the exception throwing to make clear why I do this:
Copy code
fun test2(){
val testString: String? = null
testString ?: handleUnexpectedNull()
print(testString.length)
}
private fun handleUnexpectedNull() {
throw IllegalArgumentException("null")
}
But here I get a compile error (see screenshot).
Why is this happening and can I extract the exception in a way that still supports smartcast?
I am using the jvm compiling and kotlin 1.3.72
☑️ 2
g
gildor
08/19/2020, 8:21 AM
because Kotlin doesn’t know how handleUnexpectedNull work
gildor
08/19/2020, 8:22 AM
so it cannot assume that it will always throw exception
gildor
08/19/2020, 8:22 AM
But you can make compiler know about it, change return type of handleUnexpectedNull from Unit to Nothing
👌 1
☝️ 1
h
huehnerlady
08/19/2020, 9:04 AM
@gildor aah super cool, did not know anything about nothing yet, thanks 🙂