Hi, not sure if I am right herem but as it has som...
# compiler
h
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
because Kotlin doesn’t know how handleUnexpectedNull work
so it cannot assume that it will always throw exception
But you can make compiler know about it, change return type of handleUnexpectedNull from Unit to Nothing
👌 1
☝️ 1
h
@gildor aah super cool, did not know anything about nothing yet, thanks 🙂
g
see stdlib functions such as
error()
it’s the same case
👍🏻 1