Why does Smartcast not work after nullcheck

Viewed 1360

I'm attempting advent of code and wanted to create a class for day 10. I know that the values can be null, so I declared them as nullable. At some point, I need to check whether the value is assigned and do something with it. There comes the issue. I check beforehand via high != null, but in the line that follows, I have to use !! to convince the compiler that it actually is null.

It seems that it can't find the proper compareTo method, despite nullchecking it first. I guess, it didn't smartcast my variable

private class Bot(val number: Int, var low: Int?, var high: Int?) {

  fun acceptValue(value: Int) {
    if (low == null && high == null) {
      high = value
    } else {
      if (high != null) {
        if (high!! > value) { //it doesn't compile, because appareantly, high is still not considered nonnull at this point
          low = value
        } else {
          low = high
          high = value
        }
      }
    }
  }
}

the kotlin-version is use is 1.1.3-2

Is that a bug? Am I doing something wrong?

3 Answers
Related