Let's say I have a piece of code like:
fun temp2 (li : MutableList<Int>):Int {
if (li.isEmpty()) return 0
val pos=li.filter { it>0 }
val neg=li.filter { it<0 }
if (pos.isEmpty() && neg.isNotEmpty()){
// this gives compiling error because Required: Int, Found: Int?
// But I just checked one line higher that neg is Not Empty, so there (at least I guess)
// no possible way to have an NPE?
//return neg.max()
return neg.max()!! //this works fine
}
Is there any particular reason why compiler doesn't infer that .max() can only yield an Int, and thus this should not be an error, or am I missing something? Kotlin's documentation proudly points out about Smart Casts, and I think this is a quite similar and easy inference to make?